Study/Problem Solving

[BOJ_4134] 다음 소수

monitor 2023. 11. 21. 07:03

설명


0,1의 예외처리를 잘 하지 못하여서 틀렸습니다를 여러번 봤던 문제다.

 

 

코드


import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);

        int t = sc.nextInt();
        while (t-- > 0) {
            long n = sc.nextLong();
            if(n == 0 || n == 1){
                System.out.println(2);
                continue;
            }
            while (true) {
                boolean check = true;
                for (long i = 2; i <= Math.sqrt(n); i++) {
                    if (n % i == 0) {
                        check = false;
                        break;
                    }
                }
                if (check) {
                    System.out.println(n);
                    break;
                }
                n++;
            }
        }
    }
}

 

 

4134번: 다음 소수

첫째 줄에 테스트 케이스의 개수가 주어진다. 각 테스트 케이스는 한 줄로 이루어져 있고, 정수 n이 주어진다.

www.acmicpc.net