首页 > 代码库 > 【POJ2739】Sum of Consecutive Prime Numbers
【POJ2739】Sum of Consecutive Prime Numbers
简单的素数打表,然后枚举。开始没注意n读到0结束,TLE了回。。下次再认真点。A过后讨论里面有个暴力打表过的,给跪了!
1 #include <iostream> 2 #include <cstdlib> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 #include <cctype> 7 #include <complex.h> 8 #include <cmath> 9 using namespace std;10 11 const int N = 10005;12 13 bool is[N]; int prm[N];14 int getprm (int n) {15 int i, j, k = 0;16 int s, e = (int)(sqrt(0.0 + n) + 1);17 memset (is, 1, sizeof (is));18 prm[k ++] = 2; is[0] = is[1] = 0;19 for (i = 4; i < n; i += 2) is[i] = 0;20 for (i = 3; i < e; i += 2)21 if (is[i]){22 prm[k ++] = i;23 for (s = i * 2, j = i * i; j < n; j += s) {24 is[j] = 0;25 }26 }27 for (; i < n; i += 2) {28 if (is[i]) prm[k ++] = i;29 }30 return k;31 }32 33 int main () {34 getprm(10000);35 /*for (int i = 0 ; i < 100; ++ i) {36 cout << i << ": ";37 cout << prm[i] << endl;38 }*/39 // 1229个素数 0-1w40 ios::sync_with_stdio(false);41 cin.tie(0);42 int n;43 while (~scanf ("%d", &n)) {44 if (n == 0) break;45 int sum, cnt = 0;46 for (int i = 0 ; prm[i] <= n; ++ i) {47 sum = 0;48 for (int j = i; prm[j] <= n; ++ j) {49 sum += prm[j];50 if (sum < n) {51 continue;52 } else if (sum == n) {53 cnt ++;54 break;55 } else {56 break;57 }58 }59 }60 printf ("%d\n", cnt);61 }62 return 0;63 }
【POJ2739】Sum of Consecutive Prime Numbers
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。