toj2010 Sum of Consecutive Prime Numbers
来源:互联网 发布:百度网盘 不限速 知乎 编辑:程序博客网 时间:2024/06/03 17:29
题目链接:http://acm.tju.edu.cn/toj/showp2010.html
题目大意:求满足连续素数数的和等于某个数的序列个数。
思路:先打素数表 在遍历素数表计算 本题主要是教会我们打素数表
代码:
//toj 2010 求满足连续素数数的和等于某个数的序列个数:先打素数表 在遍历素数表计算
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
int prime[1229];
int main()
{
int n, res;
int i, j, k, flag, sum;
res=0;
for(i=2; i<=10000; i++) //判断素数 构造素数表
{
//int sq=sqrt(i*1.0);
flag=0;
for(j=2; j<=sqrt(i); j++)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==0)
prime[res++]=i;
}
while(cin>>n&&n)
{
k=0;
for(i=0; i<res&&prime[i]<=n; i++)//prime[i]<=n可以加强条件 //遍历素数表
{
sum = prime[i];
j=i+1;
while(sum<n&&j<res)
sum+=prime[j++];
if(sum==n)
k++;
}
cout<<k<<endl;
}
return 0;
}
- toj2010 Sum of Consecutive Prime Numbers
- toj2010 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
- 设计模式C++实现(8)——代理模式
- 查看cpu几核
- 构造函数
- 设计模式C++实现(9)——享元模式
- CODE 18: Distinct Subsequences
- toj2010 Sum of Consecutive Prime Numbers
- 设置服务器以太网Bonding
- 设计模式C++实现(10)——桥接模式
- php循环语句与函数
- 异或操作交换两个变量的值
- 【专题】Subsequence
- java api 帮助文档 chm 1.5 1.6 中文版英文版.
- Exchange the numbers of row and array of a two-dimensional array, and form a new two-dimensional ar
- oracle笔记7--存储过程触发器