NYOJ之素数求和问题

----------------------------------------------

这道题有坑,虽然明确说<1000,但是实际上是<=1000。

AC代码:

 1 import java.util.Scanner;
 2 
 3 public class Main {
 4 
 5     public static void main(String[] args) {
 6         
 7         preprocess();
 8         
 9         Scanner sc=new Scanner(System.in);
10         
11         int times=sc.nextInt();
12         while(times-->0){
13             int n=sc.nextInt();
14             int ans=0;
15             while(n-->0){
16                 int t=sc.nextInt();
17                 if(prime[t]) ans+=t;
18             }
19             System.out.println(ans);
20         }
21     }
22     
23     private static boolean prime[]=new boolean[1001];
24     
25     public static void preprocess(){
26         for(int i=2;i<prime.length;i++){
27             prime[i]=true;
28         }
29         for(int i=2;i<prime.length;i++){
30             if(prime[i]){
31                 for(int j=i*2;j<prime.length;j+=i){
32                     prime[j]=false;
33                 }
34             }
35         }
36     }
37     
38 }

题目来源: http://acm.nyist.net/JudgeOnline/problem.php?pid=22

素数求和问题

原文地址:https://www.cnblogs.com/cc11001100/p/5791703.html