zoukankan      html  css  js  c++  java
  • 素数求和问题 (今天见到了一个很不错的标记方法)

    
    

    素数求和问题    http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=22

    时间限制: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


    package 算法入门; import java.util.Scanner;
    public class Nyist22Twice { public static int[]arr = new int[1010]; public static void main(String[] args) { fun(); //把从 1到1010之间的非素数找出来标记为1,其余的就是素数 Scanner cin = new Scanner(System.in); int a = cin.nextInt(); while(a-->0){ int m = cin.nextInt(); int total = 0; while(m-->0){ int n = cin.nextInt(); if(arr[n]==0) total += n; //标记为0的是素数 } System.out.println(total); } } private static void fun() { int m = (int)Math.sqrt(1010)+1; arr[1]=arr[0]=1; for(int i = 2;i<=m;i++){ if(arr[i]==0){ for(int j = i*i;j<=1009;j+=i){ arr[j]=1; } } } } }
  • 相关阅读:
    百度API车牌识别——Restful方式
    cxgrid 满足条件颜色显示
    cxgrid 增加右键菜单
    折线图堆积图
    echarts 堆积图
    echarts 柱型图和折线图组合
    图表自动轮播
    Delphi D10.3 FMX android 调用自带浏览器打开网页
    echarts-JSON请求数据
    堆叠条形图
  • 原文地址:https://www.cnblogs.com/qjack/p/3463001.html
Copyright © 2011-2022 走看看