SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
[코드]
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class CodingTest {
static int[] number;
static int max; // 최대값 저장
static int trade;
static String result;
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for (int test_case = 1; test_case <= T; test_case++) {
String num = sc.next();
number = new int[num.length()];
for (int i = 0; i < num.length(); i++) {
number[i] = num.charAt(i) - '0';
}
trade = sc.nextInt(); // 교환 횟수
if (trade > number.length)
trade = number.length;
dfs(0, 0); // dfs 시작
System.out.println("#" + test_case + " " + max);
max = 0;
}
}
static void dfs(int start, int count) {
int temp = 0;
if (trade == count) {
result = "";
for (int n : number)
result += String.valueOf(n); // 문자열로 붙여넣기
max = Math.max(max, Integer.parseInt(result)); // 최대값
return;
}
for (int i = start; i < number.length; i++) {
for (int j = i + 1; j < number.length; j++) {
temp = number[i];
number[i] = number[j];
number[j] = temp;
dfs(i, count + 1); // 깊이 +1
// 원래 자리로
temp = number[i];
number[i] = number[j];
number[j] = temp;
}
}
}
}'Study > Test(Java)' 카테고리의 다른 글
| [SWEA] 1215 [S/W 문제해결 기본] 3일차 - 회문1 Java (0) | 2024.05.15 |
|---|---|
| [SWEA] 2805 농작물 수확하기 Java (0) | 2024.05.14 |
| [SWEA] 1289 원재의 메모리 복구하기 Java (0) | 2024.05.14 |
| [SWEA] 1208 [S/W 문제해결 기본] 1일차 - Flatten Java (0) | 2024.05.14 |
| [SWEA] 1206 [S/W 문제해결 기본] 1일차 - View Java (0) | 2024.05.14 |