-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecreasingNumber.java
43 lines (32 loc) · 1.04 KB
/
DecreasingNumber.java
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
32
33
34
35
36
37
38
39
40
41
42
43
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
public class DecreasingNumber {
private static List<Long> resultList = new LinkedList<>();
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
if(N <= 10) {
System.out.println(N);
return;
}
if(N > 1022) {
System.out.println(-1);
return;
}
for(int index = 0; index < 10; index++) {
mkResultList(index, 1);
}
Collections.sort(resultList);
System.out.println(resultList.get(N));
}
private static void mkResultList(long number, int index) {
if(index > 10) return;
resultList.add(number);
for(int add = 0; add < number % 10; add++) {
mkResultList(number * 10 + add, index + 1);
}
}
}