Skip to content

Commit 2ff69f0

Browse files
committed
[Silver I] Title: 숫자 재배치, Time: 196 ms, Memory: 15572 KB -BaekjoonHub
1 parent 6e50c28 commit 2ff69f0

2 files changed

Lines changed: 81 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# [Silver I] 숫자 재배치 - 16943
2+
3+
[문제 링크](https://www.acmicpc.net/problem/16943)
4+
5+
### 성능 요약
6+
7+
메모리: 15572 KB, 시간: 196 ms
8+
9+
### 분류
10+
11+
수학, 브루트포스 알고리즘, 백트래킹
12+
13+
### 제출 일자
14+
15+
2025년 8월 1일 23:52:15
16+
17+
### 문제 설명
18+
19+
<p>두 정수 A와 B가 있을 때, A에 포함된 숫자의 순서를 섞어서 새로운 수 C를 만들려고 한다. 즉, C는 A의 순열 중 하나가 되어야 한다. </p>
20+
21+
<p>가능한 C 중에서 B보다 작으면서, 가장 큰 값을 구해보자. C는 0으로 시작하면 안 된다.</p>
22+
23+
### 입력
24+
25+
<p>첫째 줄에 두 정수 A와 B가 주어진다.</p>
26+
27+
### 출력
28+
29+
<p>B보다 작은 C중에서 가장 큰 값을 출력한다. 그러한 C가 없는 경우에는 -1을 출력한다.</p>
30+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
6+
static List<Integer> num;
7+
static int B, C, SIZE;
8+
static boolean[] visited;
9+
static int[] selecteds;
10+
static StringTokenizer st;
11+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
12+
public static void main(String[] args) throws Exception{
13+
inputSetting();
14+
perm(0, 0);
15+
System.out.println(C);
16+
}
17+
18+
static void perm(int cnt, int select){
19+
if(cnt == SIZE){
20+
if(B <= select || SIZE != (int) Math.log10(select) + 1) return;
21+
C = Math.max(C, select);
22+
return;
23+
}
24+
25+
for(int i = 0; i < SIZE; i++){
26+
if(visited[i]) continue;
27+
28+
visited[i] = true;
29+
select = select * 10 + num.get(i);
30+
perm(cnt + 1, select);
31+
select = (select - num.get(i)) / 10;
32+
visited[i] = false;
33+
}
34+
}
35+
36+
static void inputSetting() throws Exception{
37+
st = new StringTokenizer(br.readLine());
38+
39+
int a = Integer.parseInt(st.nextToken());
40+
B = Integer.parseInt(st.nextToken());
41+
C = -1;
42+
43+
num = new ArrayList<>();
44+
while(0 < a){
45+
num.add(a % 10);
46+
a /= 10;
47+
}
48+
SIZE = num.size();
49+
visited = new boolean[SIZE];
50+
}
51+
}

0 commit comments

Comments
 (0)