POJ2749 Sum of Consecutive Prime Numbers
2012-09-30 11:21
232 查看
题目大意:求出一个数可以有几个连续的素数和组成。 思路:水题~~~不过有一点要注意~~10000内的素数最多有1300个,所以即使不用在线打表穷举都无压力,只是10^6执行次数而已。但是如果此题改为n<=10^5也许会再卡掉一些人,10万内素数个数为9900个左右,即差不多10^4个,在穷举的O(n)算法中应该会TIE,所以,在线打表更为理想,虽然耗时多了30ms。 AC program: #include<stdio.h> #include<iostream> #include<math.h> #include<string.h> #include<algorithm> using namespace std; int pp[1500],kg; int ff[10005]; int nn[10005]; int main() { int kg=0; for(int i=2;i<=10000;i++) { if(!ff[i]) { pp[kg++]=i; for(int g=i*i;g<=10000;g+=i) ff[g]=1; } } int sum; for(int i=0;i<kg;i++) { sum=pp[i]; for(int g=i+1;g<kg;g++) { sum+=pp[g]; //让你的数组滚动起来 if(sum>10000)break; nn[sum]++; } } int n; while(cin>>n,n) { if(!ff ) cout<<nn +1<<endl; else cout<<nn <<endl; /// 在上面的打表中没有把单个的素数打出来,此处稍加判断 } //system("pause"); return 0;}
相关文章推荐
- poj 2739 Sum of Consecutive Prime Numbers
- poj 2739 Sum of Consecutive Prime Numbers
- POJ 2739 Sum of Consecutive Prime Numbers(素数打表水题)
- poj 2739 Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers----暴力水题
- POJ-2739-Sum of Consecutive Prime Numbers
- POJ 2739 Sum of Consecutive Prime Numbers
- POJ2739 Sum of Consecutive Prime Numbers素数打表
- 1.3 SUM OF Consecutive Prime Numbers
- 尺取法入门 POJ 2739 Sum of Consecutive Prime Numbers
- POJ 2739 Sum of Consecutive Prime Numbers【素数打表】
- UVa 1210 - Sum of Consecutive Prime Numbers
- POJ-2739 Sum of Consecutive Prime Numbers
- poj 2739 Sum of Consecutive Prime Numbers(暴力+打表)
- 2739 -- Sum of Consecutive Prime Numbers
- POJ2739——DFS——Sum of Consecutive Prime Numbers
- UVa1210 Sum of Consecutive Prime Numbers
- POJ 2739 Sum of Consecutive Prime Numbers
- UVa 1210 - Sum of Consecutive Prime Numbers(滑动窗口)
- POJ 2739 Sum of Consecutive Prime Numbers