본문 바로가기
Algorithm/BaekJoon[Java]

백준 1978

by 씨니 2022. 4. 1.
728x90

▶ 1978문제 - 소수찾기

 

▶ 풀이방법

import java.io.*;
import java.util.*;
public class Main {
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		Stack<Integer> stack = new Stack<>();
		
		int n = Integer.parseInt(br.readLine());
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		for(int i = 0; i < n; i++) stack.push(Integer.parseInt(st.nextToken()));
		
		int result = 0; //소수개수 담을 공간
		for(int i = 0; i < n; i++) {
			int num = stack.pop();
			int ck = 0;
			for(int j = 2; j < num; j++) {
				if(num % j == 0) {
					ck++;
					break;
				}
			}
			if(num != 0 && num != 1 && ck == 0) result++;
		}
		bw.write(String.valueOf(result));
		bw.flush();
		bw.close();
		br.close();
	}
}
728x90

'Algorithm > BaekJoon[Java]' 카테고리의 다른 글

백준 10872  (0) 2022.04.01
백준 1929  (0) 2022.04.01
백준 11576  (0) 2022.04.01
백준 1212  (0) 2022.04.01
백준 1373  (0) 2022.04.01