쵼쥬 2021. 11. 25. 14:39


풀이 방법

감소하는 수의 최대는 9876543210이기 때문에 최대 수가 나올때까지 모든 수를 구해주고 순서대로 정렬해주었다.

 

내 코드

package com.company;

import java.io.*;
import java.util.*;

public class Main {
    static int N;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        N = Integer.parseInt(br.readLine());

        ArrayList<String> list = new ArrayList<>();

        for (long i = 0; i < 10; i++) {
            list.add(i + "");
        }

        String x = "";
        int index = 0;

        while (!x.equals("9876543210")) {
            x = list.get(index);
            int temp = Integer.parseInt(x.charAt(0) + "");
            for (long i = temp + 1; i < 10; i++) {
                list.add(i + x);
            }
            index++;
        }

        Collections.sort(list, (s1, s2) -> {
            if (Long.parseLong(s1) - Long.parseLong(s2) > 0)
                return 1;
            return -1;
        });

        System.out.println(N >= list.size() ? -1 : list.get(N));
    }
}