POJ2749 Sum of Consecutive Prime Numbers
来源:互联网 发布:迅雷赚钱宝监控软件 编辑:程序博客网 时间:2024/06/15 13:08
题目大意:求出一个数可以有几个连续的素数和组成。思路:水题~~~不过有一点要注意~~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[n]) cout<<nn[n]+1<<endl; else cout<<nn[n]<<endl; /// 在上面的打表中没有把单个的素数打出来,此处稍加判断 } //system("pause"); return 0;}
- POJ2749 Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- POJ 2739 Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers pku 2739
- POJ2739 Sum of Consecutive Prime Numbers
- poj 2739 Sum of Consecutive Prime Numbers
- 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
- POJ 2739 Sum of Consecutive Prime Numbers
- POJ2739 Sum of Consecutive Prime Numbers
- mongodb学习笔记一
- java读取与写入Excel文件
- js 获取函数的所有参数和遍历JavaScript某个对象所有的属性名称和值的方法
- Python边学边用--BT客户端实现之BitTorrent文件解析
- GDI+自学(3)
- POJ2749 Sum of Consecutive Prime Numbers
- ASCII码字符加密解密
- Android实现甘特图(GanttChart)效果
- tcp/ip学习笔记
- 数据库分页查询
- 创建package和查看package的依赖关系
- ungetc 函数
- JavaScript 闭包究竟是什么
- C# 激活 非活动窗口 并自动 复制 粘贴 回车