백준 3052번  www.acmicpc.net/problem/3052

 

 

 

풀이

 

Collection 두 개를 이용해서 input arraylist 에는 입력값을 받고, remainder arraylist 에는 42로 나눈 나머지 값을 받았다. 중첩 for문을 이용해서 remainder 의 인덱스 값이 그 후의 인덱스 값과 동일한 경우에는 cnt를 증가시켰다.이 때, 입력값과 동일하게 나올 수 있는 나머지 값이 총 10개 이므로 10개에서 이 cnt 를 제외한 값을 정답으로 출력했다.(구하고자 하는 값은 서로 다른 나머지 수이기 때문에)

 

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
import java.util.*;
 
public class Array_3052 {
 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        List<Integer> input = new ArrayList<>();
        List<Integer> remainder = new ArrayList<>();
        int cnt = 0;
        
        for (int i=0; i<10; i++) {
            input.add(sc.nextInt());
            remainder.add(input.get(i)%42);
        }
        sc.nextLine();
        
        for (int i=0; i<remainder.size()-1; i++) {
            for (int j=i+1; j<remainder.size(); j++) {
                if(remainder.get(i)==remainder.get(j)) {
                    cnt++;
                    break;
                }
            }
        }
        
        System.out.println(10-cnt);
    
        sc.close();
    }
}
cs

 

댓글