L1-028 判断素数 java语言

L1-028 判断素数 (10 分)
本题的目标很简单,就是判断一个给定的正整数是否素数。

输入格式:
输入在第一行给出一个正整数N(≤ 10),随后N行,每行给出一个小于2
​31
​​ 的需要判断的正整数。

输出格式:
对每个需要判断的正整数,如果它是素数,则在一行中输出Yes,否则输出No。

输入样例:
2
11
111
输出样例:
Yes
No

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int a[] = new int[N];
		for (int i = 0; i < N; i++) {
			a[i] = sc.nextInt();
		}
		for (int i = 0; i < N; i++) {
			sushu(a[i]);
		}
	}
	public static void sushu(int n) {
		int count=0;
		if (n <= 3) {
			System.out.println("Yes");
		} else {
			for (int i = 2; i <= Math.sqrt(n); i++) {
				if (n % i == 0) {
					count++;
					break;
				}
			}
			if(count!=0) {
			System.out.println("No");
			}else {
				System.out.println("Yes");
			}
		}
	}

}

猜你喜欢

转载自blog.csdn.net/an_uoh_en/article/details/87896557
今日推荐