2 초 | 128 MB | 59692 | 27992 | 23029 | 48.221% |
주어진 수 N개 중에서 소수가 몇 개인지 찾아서 출력하는 프로그램을 작성하시오.
첫 줄에 수의 개수 N이 주어진다. N은 100이하이다. 다음으로 N개의 수가 주어지는데 수는 1,000 이하의 자연수이다.
주어진 수들 중 소수의 개수를 출력한다.
답안
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.util.*;
public class Main {
//baekjoon #1978
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int times = Integer.parseInt(bf.readLine());
boolean[] table = new boolean[1001];
table[2] = true;
for(int i = 2; i < table.length; i++){
for(int j = 2; j < i; j++){
if(i%j==0)break;
if(j==i-1)table[i]=true;
}
}
int result = 0;
StringTokenizer st = new StringTokenizer(bf.readLine()," ");
for(int i = 0; i < times; i++){
int input = Integer.parseInt(st.nextToken());
if(table[input])result++;
}
System.out.println(result);
}
}
백준 알고리즘 11653번 : 소인수분해 (0) | 2021.01.10 |
---|---|
백준 알고리즘 2581번 : 소수 (0) | 2021.01.10 |
백준 알고리즘 1011번 : Fly me to the Alpha Centauri (0) | 2021.01.09 |
백준 알고리즘 10757번: 큰 수 A+B (0) | 2021.01.09 |
백준 알고리즘 2839번 : 설탕 배달 (0) | 2021.01.09 |