标签:
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 Main; import java.util.Scanner; public class Main2 { public static void main(String[] args) { Scanner input = new Scanner(System.in); int num = input.nextInt(); for (int i = 0;i<num;i++) { int n = input.nextInt(); int [] array = new int[n]; for (int j = 0;j<n;j++) { array[j] = input.nextInt(); } int result =cal(array); System.out.println(result); } } private static int cal(int[] list) { int sum = 0; for (int i = 0;i<list.length;i++) { if (isPrime(list[i])) { sum += list[i]; } } return sum; } public static boolean isPrime(int a) { boolean flag = true; if (a < 2) {// 素数不小于2 return false; } else { for (int i = 2; i <= Math.sqrt(a); i++) { if (a % i == 0) {// 若能被整除,则说明不是素数,返回false flag = false; break;// 跳出循环 } } } return flag; } }
标签:
原文地址:http://www.cnblogs.com/airycode/p/5311739.html