forked from wotjd4305/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswe1244.java
More file actions
111 lines (89 loc) · 2.41 KB
/
swe1244.java
File metadata and controls
111 lines (89 loc) · 2.41 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import java.util.*;
public class swe1244 {
static Scanner sc = new Scanner(System.in);
static String answer = "";
static int max = 0;
static int possibleMax =0;
public static void main(String[] args) {
int t = sc.nextInt();
for (int T = 1; T <= t; T++) {
String str = sc.next();
int exN = sc.nextInt();
answer = "";
max =0;
possibleMax = findMax(str);
dfs(str, 0,exN);
answer = ""+max;
System.out.println("#" + T + " " + answer);
}
}
public static int findMax(String str)
{
ArrayList<Integer> a = new ArrayList<>();
String buffer= "";
for(int i=0; i<str.length(); i++)
{
a.add(Integer.parseInt( "" + str.charAt(i)));
}
Collections.sort(a,Collections.reverseOrder());
for(int i : a)
buffer += i +"";
return Integer.parseInt(buffer);
}
public static void dfs(String str,int cnt, int exN)
{
String buffer = "";
if((str.length()/2 < cnt) && ((cnt % 2) == 0))
{
max = Math.max(max, Integer.parseInt(str));
return;
}
if(cnt == exN)
{
//System.out.print("MAX");
max = Math.max(max, Integer.parseInt(str));
return;
}
//뱅뱅돌때
if(max == Integer.parseInt(str))
{
return;
}
//가능한 최대값일떄
if(possibleMax == max)
{
return;
}
for(int i =0; i<str.length(); i++)
{
for(int j=i+1; j<str.length(); j++) {
if(i==j)
break;
buffer = swap(str, i, j);
dfs(buffer, cnt+1, exN);
}
}
}
public static String swap(String str, int p1, int p2)
{
String buffer = "";
//System.out.print(" swap - ");
for(int i =0; i<str.length(); i++)
{
//바뀌는 부분은 냅두고
if(i==p1)
{
buffer += str.charAt(p2);
continue;
}
if(i==p2)
{
buffer += str.charAt(p1);
continue;
}
buffer += str.charAt(i);
}
// System.out.print("버퍼값" + buffer);
return buffer;
}
}