BOJ_1978_소수찾기 (JAVA)

Swimming_Ram·2025년 7월 20일
0

Javalgorithm

목록 보기
5/7


import java.io.*;
import java.util.*;

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int testcase_num = Integer.parseInt(br.readLine());
		int answer_count = 0;
		
		String[] str_num = br.readLine().split(" ");
		int[] num_list = new int[str_num.length];
		
		for (int i = 0; i < str_num.length; i++) {
			num_list[i] = Integer.parseInt(str_num[i]);
		}
		
		for (int j = 0; j < num_list.length; j++) {
			if (isPrime(num_list[j])) {
				answer_count ++;
			}
		}
		System.out.println(answer_count);
		

	}
	
	public static boolean isPrime(int num) {
		if (num < 2) return false;
		if (num == 2) return true;
		
		for (int i = 2; i * i <= num; i++) {
			if (num % i == 0) {
				return false;
			}
		}
		return true;
	}

}

메서드를 끌어와서 사용하는 코드를 작성해보았다.
아직까지 자바코드 사용은 어색함이 많다 구현력이 많이 부족하니 배열, 메서드 같은 기능들을 자유자재로 사용할때까지 연습해야 겠다.

profile
Swimming is good at loss Weight

0개의 댓글