본문 바로가기

IT/알고리즘

백준 9단계(9020)

반응형
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
 
public class Main {
    //3
    //8
    //10
    //16
    
    //result 
    //3 5
    //5 5
    //5 11
    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        
        int repeat = Integer.parseInt(br.readLine());
        
        while(repeat > 0) {
            repeat--;
            int n = Integer.parseInt(br.readLine());
            boolean[] bool = new boolean[n];
            //sieve of Eratosthenes or 에라토스테네스의 체
            //모든 숫자를 true값을 지정해준다
            for(int i = 2; i < bool.length; i++) {
                bool[i] = true;
            }
            //eratosthenes 알고리즘으로 소수 아닌 숫자들은 false
            //처음으로 2의배수, 그리고 3의배수, 4의배수 다음 5의배수, n의 제곱근까지
            for(int i = 2; i < Math.sqrt(n); i++) {
                if(bool[i]) {
                    for(int j = i * i; j < n; j += i) {
                        bool[j] = false;
                    }
                }
            }
            //중간부터 시작
            int firstIndex = n/2;
            int secondIndex = n/2;
            
            while(true) {
                if(bool[firstIndex] && bool[secondIndex]) break;
                firstIndex --;
                secondIndex ++;
            }
 
            bw.write(String.valueOf(firstIndex) + " " + String.valueOf(secondIndex) + "\n");
        
        }
        bw.flush();
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
 

remains unsolved number theory, goldbach's conjecture

반응형

'IT > 알고리즘' 카테고리의 다른 글

백준 9단계(4948)  (0) 2019.12.22
백준 9단계(1929)  (0) 2019.12.22
백준 9단계(2581)  (0) 2019.12.22
백준 9단계(1978)  (0) 2019.12.21
백준 8단계(1011)  (0) 2019.12.21