Skip to content

Commit b252698

Browse files
authored
[20250907] BOJ / G4 /중복없는 수 / 이종환
1 parent 08c1b1d commit b252698

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
```
2+
import java.io.IOException;
3+
import java.io.*;
4+
import java.util.*;
5+
6+
7+
public class Main {
8+
9+
static int[] max = {0,9,98,987,9876,98765,987654,9876543,98765432,987654321};
10+
static int[] min = {0,1,12,123,1234,12345,123456,1234567,12345678,123456789};
11+
static int ans,target;
12+
static int[] num;
13+
14+
15+
static boolean[] used = new boolean[10];
16+
17+
public static void main(String[] args) throws IOException {
18+
Scanner sc = new Scanner(System.in);
19+
20+
while(sc.hasNext()) {
21+
target = sc.nextInt();
22+
ans = -1;
23+
process();
24+
print();
25+
}
26+
sc.close();
27+
}
28+
29+
public static void init() throws IOException {
30+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
31+
ans = -1;
32+
target = Integer.parseInt(br.readLine());
33+
34+
}
35+
36+
public static void process() throws IOException {
37+
if (target >= max[9]) {
38+
ans = 0;
39+
return;
40+
}
41+
42+
43+
int size = 0;
44+
45+
for (int i = 8; i >= 0; i--) {
46+
if (target / Math.pow(10, i) >= 1) {
47+
size = i+1;
48+
break;
49+
}
50+
}
51+
52+
if (target >= max[size]) {
53+
ans = min[size+1];
54+
return;
55+
}
56+
57+
num = new int[size];
58+
59+
60+
recur( 0, size);
61+
62+
}
63+
64+
private static void recur(int cur, int goal) {
65+
if (ans != -1) return;
66+
67+
if (cur == goal) {
68+
int res = 0;
69+
for (int i = 0; i < goal; i++) {
70+
res*=10;
71+
res+=num[i];
72+
}
73+
ans = res;
74+
return;
75+
}
76+
77+
int original = (target / (int)Math.pow(10, goal - cur - 1)) % 10;
78+
int nNum = -1;
79+
80+
for (int i = 1; i <= 9; i++) {
81+
if (!used[i] && i >= original) {
82+
used[i] = true;
83+
num[cur] = i;
84+
recur(cur+1, goal);
85+
used[i] = false;
86+
}
87+
}
88+
89+
90+
}
91+
92+
93+
94+
95+
96+
97+
98+
public static void print() {
99+
System.out.println(ans);
100+
}
101+
```
102+
}

0 commit comments

Comments
 (0)