본문 바로가기
코딩테스트/백준

[백준/3052] 나머지 (Java)

by wo__ongii 2024. 12. 27.
728x90

 

1. Set 사용

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;

public class Day22 {
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        Set<Integer> newArr = new HashSet<>();
        for(int i=0;i<10;i++){
            int num = Integer.parseInt(br.readLine());
            int A = num % 42;
            if(!newArr.contains(A)){
                newArr.add(A);
            }
        }
        br.close();
        System.out.println(newArr.size());
    }
}

 

2. Stream 사용

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int[] arr = new int[10];
        for(int i=0;i<10;i++){
            int num = Integer.parseInt(br.readLine());
            arr[i] = num % 42;
        }
        //distinct() 메소드로 중복 제거
        arr = Arrays.stream(arr).distinct().toArray();
        br.close();
        System.out.println(arr.length);
    }
}

 

728x90
반응형