umilove98의 블로그

백준 3711 Java 자바 본문

algorithm/백준

백준 3711 Java 자바

umilove98 2021. 8. 31. 12:24
반응형

 

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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
 
public class Q3711 {
 
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());    // test case의 개수
 
        for(int k = 0; k < n; k++) {    
            
            int g = Integer.parseInt(br.readLine());    // 학생 수 입력
            int[] arr = new int[g];                        // 각 학생의 학번을 저장할 배열 생성
            for(int i = 0; i < g; i++) {
                arr[i] = Integer.parseInt(br.readLine());
            }
            
            int m = 0;                                    // 답 m 을 찾을 변수 선언
            while(true) {
                m++;
                boolean istrue = true;                    // 현재 m 이 모든 학번을 나눈 나머지가 각각 다른지 판단
                ArrayList<Integer> modlist = new ArrayList<Integer>();    // 학번을 m으로 나눈 나머지를 저장할 리스트
                for(int i = 0; i < g; i++) {            // 모든 학번을 순회하며 m으로 나눔
                    if(modlist.contains(arr[i] % m)) {    // m으로 나눈 나머지가 이미 나온 값인지 판단
                        istrue = false;
                        break;
                    }
                    modlist.add(arr[i] % m);
                }
                if(istrue == true) {
                    System.out.println(m);
                    break;
                }
            }
        }
    }
 
}
 
cs
반응형

'algorithm > 백준' 카테고리의 다른 글

백준 5532 Java 자바  (0) 2021.09.03
백준 4335 Java 자바  (0) 2021.09.03
백준 2455 Java 자바  (0) 2021.08.30
백준 2435 Java 자바  (0) 2021.08.30
백준 2204 Java 자바  (0) 2021.08.29