728x90

 

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
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class BeakJoon_2981 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 
        int N = Integer.parseInt(br.readLine());
 
        StringBuilder sb = new StringBuilder();
        int [] arr = new int[N];
 
        for(int i = 0; i < N; i++){
            arr[i] = Integer.parseInt(br.readLine());
        }
 
        int val = Math.abs(arr[0- arr[1]);
 
        for(int i = 1; i < N-1  ; i++){
            val = gcb(val,Math.abs(arr[i] - arr[i+1]));
        }
 
 
        for(int i = 2; i <= val/2; i ++){
            if(val % i == 0){
                sb.append(i + " ");
            }
        }
        sb.append(val);
        System.out.println(sb);
    }
 
    static int gcb(int a,int b) {
 
        while(b != 0 ){
            int r = a%b;
            a = b;
            b = r;
        }
 
        return a;
    }
}
 
cs

=> 정답률이 낮은 편이다. 최대 공약수를 구하는 방법과 유클리드 호제법을 알아야 풀기가 조금은 수월할거같다.

728x90

'Dev > 알고리즘 ,자료구조' 카테고리의 다른 글

[프로그래머스]모음 사전(JAVA)  (0) 2021.10.12
[프로그래머스]프렌즈 4블록(JAVA)  (0) 2021.10.12
[Algorithm]BeakJoon_1541  (0) 2021.07.18
[Algorithm]BeakJoon_11399  (0) 2021.07.18
[Algorithm]BeakJoon_1931  (0) 2021.07.18