[POJ]2739-Sum of Consecutive Prime Numbers
来源:互联网 发布:程序员去汽车厂 编辑:程序博客网 时间:2024/06/18 00:22
About me:
女。某211软件工程大二生。打算从刷水题开始练手感以备战蓝桥杯省赛。代码渣渣一枚orz。欢迎路过的各位大神拍砖!!
本题题意:
输入一个2到10000之间的数(结束时以0结尾),输出这个数可以是多少组连续的素数之和。如17=2+3+5+7、41=11+13+17=2+3+5+7+11+13、2=2。
思路:
疯狂枚举orz。从2开始,判断2+3+5+7+......是否等于输入的数。若素数之和大于该数,则从下一个素数开始(如3)继续疯狂累加。若找到一组,则count++。最后输出count。
心得:
1.判断某数x是否为素数时,其约数控制在大于等于2小于根号x即可,节约运算时间。(Time Limit Exceeded的教训orz)
#include <iostream>#include<string>#include<math.h>using namespace std;int GetPrimeNumber(int x){ int i; int ok=x; for(i=2;i<=(int)sqrt((double)x);i++) { if(x%i==0) { ok=0; break; } } return ok;}int main(int argc, const char * argv[]) { int n; int sum=0,count=0; int i=2; int start=2; while(cin>>n&&n) { for(;i<=n;i++) { if(GetPrimeNumber(i)) sum+=GetPrimeNumber(i); if(sum==n) { count++; start++; while(!GetPrimeNumber(start)) { start++; } i=start-1; sum=0; } if(sum>n) { start++; while(!GetPrimeNumber(start)) { start++; } i=start-1; sum=0; } } cout<<count<<endl; sum=0; count=0; start=2; i=2; } return 0;}
欢迎各路大神拍砖!!
0 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
- 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
- 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
- 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
- POJ-2739-Sum of Consecutive Prime Numbers
- POJ 2739 Sum of Consecutive Prime Numbers
- HDU 1083 Dinic最大流模板
- HDU1969-Pie
- POJ 3366 Deli Deli 可能会
- MDK环境下stm32实现printf函数
- 面试题16:反转链表
- [POJ]2739-Sum of Consecutive Prime Numbers
- ural1014 Product of Digits (枚举)
- 最小生成树---->prim算法的应用 hdu1863
- PAT A1056. Mice and Rice (25)
- ubuntu16.04忘记密码处理方法
- Codeforces Round #399 (Div. 1 + Div. 2, combined)C. Jon Snow and his Favourite Number
- 各项知识链接集合(比较杂)
- [HTC Vive + Unity开发]——VRTK的研究——用手柄旋转物体(使用VRTK_Knob脚本)
- TCP/IP 多播和多播路由