zoukankan      html  css  js  c++  java
  • 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

    素数求和问题

  • 相关阅读:
    2020/5/18
    2020/5/17
    2020/5/15
    2020/5/13
    2020/5/12
    服务器环境配置五大免费主机系统
    6:运算符
    5:练习题
    4:Python的while循环
    3:Python条件语句
  • 原文地址:https://www.cnblogs.com/cc11001100/p/5791703.html
Copyright © 2011-2022 走看看