SWEA

swea 1288 [D2] 새로운 불면증 치료법 JAVA

녁이 2023. 11. 20. 19:59
728x90
반응형

풀이1 :

10개의 boolean 배열을 만들어서 해당 자리 숫자가 나오면 true로 변경해주고, cnt++ -> if(count == 10) break;

 

풀이2 :

10개의 int 배열을 만들어서 0 - 9 까지 숫자를 저장, 입력의 각 자리 숫자를 분해하고, 해당 자리 숫자가 나오면 cnt++ -> if(count == 10) break;

 


풀이1

public class Solution1288 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T;
        T=sc.nextInt();
        for(int test_case = 1; test_case <= T; test_case++)
        {
            boolean arr[] = new boolean[10];
            String A = sc.next();
            int N=Integer.parseInt(A);
            int count = 0;
            int num = 1;  // N에 곱할 배수
            while(true){
                for(int i=0; i<A.length(); i++){
                    int temp = Integer.parseInt(A.substring(i, i+1));  // temp = i 자리
                    if(arr[temp] == false){
                        arr[temp] = true;
                        count++;
                    }
                }
                if(count == 10)
                    break;
                else{
                    num++;
                    A=Integer.toString(N * num);
                }
            }
            System.out.println("#"+test_case+" "+A);
        }
    }

 

풀이2

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int T = sc.nextInt(); // 테스트 케이스 개수
    for (int tc = 1; tc <= T; tc++) {
        int N = sc.nextInt();
        int[] arr = new int[10]; // 0에서 9까지의 숫자를 저장하는 배열
        int cnt = 0;
        int result = 0;
        while (true) {

            // N을 다음 배수로 증가시킴
            result += N;
            int temp = result;

            // result의 각 자리수에 있는 숫자를 arr 배열에 표시
            while (temp > 0) {
                int digit = temp % 10;
                arr[digit] = 1;
                temp /= 10;
            }

            // arr 배열에 0에서 9까지 모든 숫자가 나왔는지 확인
            for (int i = 0; i < arr.length; i++) {
                if (arr[i] > 0) {
                    cnt++;
                }
            }
            if (cnt == 10) {
                break;
            }
        }
        System.out.println("#" + tc + " " + result);
    }
}

 

 

728x90
반응형