素数求和问题
时间限制:
3000 ms | 内存限制:
65535 KB
难度:
2
-
描述
-
现在给你N个数(0<N<1000),现在要求你写出一个程序,找出这N个数中的所有素数,并求和。
-
输入
-
第一行给出整数M(0<M<10)代表多少组测试数据
每组测试数据第一行给你N,代表该组测试数据的数量。
接下来的N个数为要测试的数据,每个数小于1000
输出
- 每组测试数据结果占一行,输出给出的测试数据的所有素数和 样例输入
-
3 5 1 2 3 4 5 8 11 12 13 14 15 16 17 18 10 21 22 23 24 25 26 27 28 29 30
样例输出
-
10 41 52
-
2017/5/7 23:25:17
-
目的简述:对给定的数据中的素数求和
-
1-求出素数表 查表。
-
2-判断求和
-
2017/5/7 23:52:01
-
-
#include <cstdio>
#include <memory.h>
#include <algorithm>
#include <cmath>
using namespace std;
#define L 1001
bool p[L];//prime
int pp[L];
int ppl=0;
void getpl(){//getpreme list
pp[ppl]=2;ppl=1;
memset(p,false,L*sizeof(bool));
p[2]=p[3]=true;
for(int i=3;i<L;i++){
bool state=true;
for(int j=0;j<ppl && pp[j]<= i/2;j++){
if(!(i%pp[j])) {
state=false;
break;
}
}
if(state){
p[i]=true;
pp[ppl]=i;ppl+=1;
}
}
//for(int i=0;i<=ppl;i++) printf("%d %s\n",pp[i],p[pp[i]]==true?"true":"false");
}
int main(){
getpl();
int n;
int nn;
int sum=0;
int t;
scanf("%d",&n);
while(n--){
sum=0;
scanf("%d",&nn);
while(nn--){
scanf("%d",&t);
if(p[t]){
sum+=t;//printf("##%d",t);
}
}
printf("%d\n",sum);
}
return 0;
}
-
第一行给出整数M(0<M<10)代表多少组测试数据