๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Algorithm

[Algorithm/Java][BOJ] 3052๋ฒˆ ๋‚˜๋จธ์ง€

๋ฐ˜์‘ํ˜•

[๋ฐฑ์ค€] ๋ฐฑ์ค€ 3052๋ฒˆ ๋‚˜๋จธ์ง€

https://www.acmicpc.net/problem/3052

๋ฌธ์ œ์ ‘๊ทผ

10๊ฐœ์˜ ์ž…๋ ฅ๋“ค์„ 42๋กœ ๋‚˜๋ˆ„์–ด์„œ ์„œ๋กœ ๋‹ค๋ฅธ ๋‚˜๋จธ์ง€ ๊ฐ’๋“ค์ด ๋ช‡ ๊ฐœ๊ฐ€ ์žˆ๋Š”์ง€ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์˜€๋‹ค.
์„œ๋กœ ๋‹ค๋ฅธ ๊ฐ’์ด๋ผ๋Š” ํ‚ค์›Œ๋“œ๋ฅผ ๋ณด๊ณ  Set ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋˜๊ฒ ๋‹ค๊ณ  ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค.
Set์€ ๋ฐ์ดํ„ฐ์˜ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ๋”ฐ๋กœ ์ค‘๋ณต ๊ฒ€์‚ฌ๋ฅผ ํ•˜์ง€์•Š๊ณ  ๋‚˜๋จธ์ง€๋ฅผ add ํ•ด์ฃผ๊ณ  Set์˜ ํฌ๊ธฐ๋งŒ ์ถœ๋ ฅํ•˜๋ฉด ๋˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

Code

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

public class Main {
    public static void main(String[] args) {
        HashSet<Integer> hs = new HashSet<Integer>(10);
        Scanner sc = new Scanner(System.in);
        for(int i=0; i<10; i++){
            hs.add(sc.nextInt()%42);
        }
        System.out.println(hs.size());
    }
}

์–ด๋ ค์› ๋˜ ์  / ๋ฐฐ์šด ์  / ๋Š๋‚€ ์ 

Set์ด๋ผ๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๋ชฐ๋ž๋‹ค๋ฉด ์ข€ ์–ด๋ ต๊ฒŒ ์ ‘๊ทผํ–ˆ์„ ๊ฒƒ ๊ฐ™๋‹ค.
"์„œ๋กœ ๋‹ค๋ฅธ ๊ฐ’" (์ค‘๋ณต์ด ์—†๋Š” ๊ฐ’) ์ด๋ผ๋Š” ํ‚ค์›Œ๋“œ๋ฅผ ๋ณด๊ณ  Set์„ ์ƒ๊ฐํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค,

๋ฐ˜์‘ํ˜•