본문 바로가기
Problem Solving/Baekjoon

[백준] 2774 아름다운 수 - Implementation / Java

by graycode 2025. 3. 11.

 문제 링크

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

 

 풀이 코드

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashSet;
import java.util.Set;

public class Main {

    static Set<Integer> set = new HashSet<>();

    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringBuilder sb = new StringBuilder();

        int t = read();
        while (t-- > 0) sb.append(calc(read())).append('\n');

        bw.write(sb.toString());
        bw.flush();
    }

    private static int calc(int n) {
        set.clear();
        do set.add(n % 10); while ((n /= 10) > 0);

        return set.size();
    }

    private static int read() throws IOException {
        int c, n = System.in.read() & 15;
        while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);

        return n;
    }

}

댓글