Notice
Recent Posts
Recent Comments
Link
«   2025/12   »
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
Tags more
Archives
Today
Total
관리 메뉴

jeongwon

[백준 JAVA] 3052번 - 나머지 본문

오늘의 문제

[백준 JAVA] 3052번 - 나머지

jeongwon_ 2022. 5. 27. 09:46

결국 풀지 못한 문제이다.

 

 

참고 답안 1)

package practice;

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

public class Main{
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		boolean[] arr = new boolean[42];
		
		for(int i=0; i<10; i++)
		arr[(Integer.parseInt(br.readLine()))%42] = true ;
//***나머지 자체를 index로 삼아 중복 문제 해결		
		int count = 0;
		for(boolean i : arr)
			if(i==true) count++;
		
		System.out.println(count);		
	}
}

 

참고 답안 2)

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

public class Main{
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		HashSet<Integer> hs = new HashSet<>();//중복을 허용하지 않는 HashSet
        
		for(int i=0; i<10; i++)
			hs.add(Integer.parseInt(br.readLine())%42);
		
		System.out.println(hs.size());
        
        System.out.println(hs.toString());
        //개인적으로 출력해 보았다. [84, 420, 840, 42, 252, 126] - 예제2, 중복값 84 1개

	}
}

 

여러 개의 반복문과 조건문을 썼다 지웠지만.. boolean 배열은 생각지도 못했다. 

추가로 HashSet의 편리성을 이번 기회에 절감했다.

 

답안 출처 : https://st-lab.tistory.com/46