0507 #22 NYISTOJ 素数求和问题

来源:互联网 发布:linux服务器ftp服务开 编辑:程序博客网 时间:2024/06/10 13:41

素数求和问题

时间限制:3000 ms  |  内存限制:65535 KB
难度:2
描述
现在给你N个数(0<N<1000),现在要求你写出一个程序,找出这N个数中的所有素数,并求和。
输入
第一行给出整数M(0<M<10)代表多少组测试数据
每组测试数据第一行给你N,代表该组测试数据的数量。
接下来的N个数为要测试的数据,每个数小于1000
输出
每组测试数据结果占一行,输出给出的测试数据的所有素数和
样例输入
351 2 3 4 5811 12 13 14 15 16 17 181021 22 23 24 25 26 27 28 29 30
样例输出
104152
来源: http://acm.nyist.net/JudgeOnline/problem.php?pid=22&rec=rec
2017/5/7 23:25:17
题目来源: http://acm.nyist.net/JudgeOnline/problem.php?pid=22&rec=rec
目的简述:对给定的数据中的素数求和
1-求出素数表 查表。
2-判断求和
2017/5/7 23:52:01

  1. #include <cstdio>
  2. #include <memory.h>
  3. #include <algorithm>
  4. #include <cmath>
  5. using namespace std;
  6. #define L 1001
  7. bool p[L];//prime
  8. int pp[L];
  9. int ppl=0;
  10. void getpl(){//getpreme list
  11. pp[ppl]=2;ppl=1;
  12. memset(p,false,L*sizeof(bool));
  13. p[2]=p[3]=true;
  14. for(int i=3;i<L;i++){
  15. bool state=true;
  16. for(int j=0;j<ppl && pp[j]<= i/2;j++){
  17. if(!(i%pp[j])) {
  18. state=false;
  19. break;
  20. }
  21. }
  22. if(state){
  23. p[i]=true;
  24. pp[ppl]=i;ppl+=1;
  25. }
  26. }
  27. //for(int i=0;i<=ppl;i++) printf("%d %s\n",pp[i],p[pp[i]]==true?"true":"false");
  28. }
  29. int main(){
  30. getpl();
  31. int n;
  32. int nn;
  33. int sum=0;
  34. int t;
  35. scanf("%d",&n);
  36. while(n--){
  37. sum=0;
  38. scanf("%d",&nn);
  39. while(nn--){
  40. scanf("%d",&t);
  41. if(p[t]){
  42. sum+=t;//printf("##%d",t);
  43. }
  44. }
  45. printf("%d\n",sum);
  46. }
  47. return 0;
  48. }

0 0