YoungSoo

백준 - 나머지(JAVA 풀이) 본문

코딩테스트

백준 - 나머지(JAVA 풀이)

YoungSooSoo 2022. 7. 21. 23:46

풀이

import java.util.HashSet;
import java.util.Scanner;

public class remainder {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        HashSet<Integer> hs = new HashSet<Integer>();
        int a[] = new int[10];

        for (int i = 0; i < 10; i++) {
            a[i] = sc.nextInt();
            hs.add(Integer.parseInt(Integer.toString(a[i])) % 42);
            //입력받은 값의 나머지 값을 add를 통해 HashSet에 저장
        }

        sc.close();
        System.out.print(hs.size()); //HashSet의 크기 출력
    }
}

설명

1. 10개의 값을 배열에 저장해준다.

2. HashSet을 이용하여 저장해주는데 HashSet을 이용하는 이유는 값이 중복하여 들어가지 않는다.

자세한 내용은 여기를 참고하면 된다.

https://youngsoosoo.tistory.com/39[HashSet 설명]

3. 중복되지 않는 수를 찾기 때문에 HashSet의 크기를 출력해주면 된다.