-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1038.java
More file actions
63 lines (57 loc) · 1.63 KB
/
Copy path1038.java
File metadata and controls
63 lines (57 loc) · 1.63 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// https://www.acmicpc.net/problem/1038
/*
현재수를 기준으로 다음 감소하는 수를 리턴하는 함수를 구현하고
반복문으로 n번째 감소하는 수를 구할 수 있다.
가장 큰(마지막) 감소하는 수 "9876543210"일 때는 null을 리턴한다.
*/
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader scan = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(scan.readLine());
String num = "0";
for(int i=0; i<n; i++) {
num = get_next_decending_number(num);
if(num == null) {
System.out.println(-1);
return;
}
//System.out.println(num);
}
System.out.println(num);
return;
}
// 현재 감소하는 수를 입력했을 때 다음 감소하는 수를 리턴하는 함수
public static String get_next_decending_number(String num) {
if(num.equals("9876543210")) return null;
int index = num.length()-1;
StringBuilder sb = new StringBuilder();
while(true) {
if(index == 0) {
if(num.charAt(index) == '9') {
for(int i=num.length(); i>=0; i--) {
sb.append((char)(i+'0'));
}
return sb.toString();
}
sb.append((char)(num.charAt(index)+1));
for(int i=num.length()-2; i>=0; i--) {
sb.append((char)(i+'0'));
}
return sb.toString();
}
if(num.charAt(index-1) - num.charAt(index) == 1) {
index--;
continue;
}
else {
sb.append(num.substring(0, index))
.append((char)(num.charAt(index)+1));
for(int i=num.length()-index-2; i>=0; i--) {
sb.append((char)(i+'0'));
}
return sb.toString();
}
}
}
}