-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path1244_최대 상금.java
58 lines (49 loc) · 1.42 KB
/
1244_최대 상금.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
class Solution {
public static int[] digit;
public static int n;
public static int answer;
public static int getPrize() {
int num = digit[0];
for (int i = 1; i < digit.length; i++) {
num = num * 10 + digit[i];
}
return num;
}
public static void swap(int a, int b) {
int tmp = digit[a];
digit[a] = digit[b];
digit[b] = tmp;
}
public static void exchange(int now, int cnt) {
if (cnt >= n) {
answer = Math.max(answer, getPrize());
return;
}
for (int i = now; i < digit.length; i++) {
for (int j = i + 1; j < digit.length; j++) {
swap(i, j);
exchange(i, cnt + 1);
swap(i, j);
}
}
}
public static void main(String args[]) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
for (int test_case = 1; test_case <= T; test_case++) {
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
String prize = st.nextToken();
digit = new int[prize.length()];
for (int i = 0; i < digit.length; i++) {
digit[i] = Character.getNumericValue(prize.charAt(i));
}
n = Integer.parseInt(st.nextToken());
answer = 0;
exchange(0,0);
System.out.println("#" + test_case + " " + answer);
}
}
}