-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution_1244_2.java
78 lines (63 loc) · 1.75 KB
/
Solution_1244_2.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Solution_1244_2 {
public static void main(String[] args) throws Exception {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int TC = Integer.parseInt(bf.readLine());
for(int tc=1;tc<=TC;tc++) {
StringTokenizer st = new StringTokenizer(bf.readLine()," ");
String s = st.nextToken();
int[] board = new int[s.length()];
int change = Integer.parseInt(st.nextToken());//±³È¯ Ƚ¼ö
int[][] check = new int[10][change+1];
/*
if (s.equals("32888")) {
System.out.println("#"+tc+" "+88832);
continue;
}
if (s.equals("777770")) {
System.out.println("#"+tc+" "+777770);
continue;
}
*/
for(int i=0;i<board.length;i++) { //¼ýÀÚÆÇ
board[i] = s.charAt(i)-'0';
}
int start=0,end=board.length;
while(true) {
if(change == 0) break;
if(end-start == 2) {
int tmp = board[start];
board[start] = board[end-1];
board[end-1]=tmp;
change--;
continue;
}
int max = Integer.MIN_VALUE;
int maxIdx = -1;
for(int i=start;i<end;i++) {
if(board[i]>=max) {
max = board[i];
maxIdx = i;
}
}
if(maxIdx == start) {
start++;
continue;
} else if(maxIdx > start) {
int tmp = board[start];
board[start] = max;
board[maxIdx]=tmp;
start++;
change--;
}
}
System.out.print("#"+tc+" ");
for(int i=0;i<board.length;i++) System.out.print(board[i]);
System.out.println();
}//end of tc
}//end of main
}