주어진 문제 중에서 소수가 얼마나 있는지 찾아내는 함수이다.
문제 자체는 어렵지 않았다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | package baek1978; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); int testCase = Integer.parseInt(bf.readLine()); StringTokenizer st = new StringTokenizer(bf.readLine()); int sum = 0; for (int i = 0; i < testCase; i++) { int num = Integer.parseInt(st.nextToken()); if (isSoSu(num)) { sum++; } } System.out.println(sum); } public static boolean isSoSu(int num) { if (num == 1) { return false; } for (int i = 2; i < num; i++) { if (num%i == 0) { return false; } } return true; } } | cs |
잘 풀었다.
'알고리즘' 카테고리의 다른 글
백준 알고리즘 1011 - Fly me to the Alpha Centauri (0) | 2020.03.23 |
---|---|
백준 알고리즘 2775 - 부녀회장이 되고싶어 (0) | 2020.03.21 |
백준 알고리즘 10250 - ACM 호텔 (0) | 2020.03.20 |
댓글