Skip to content

Commit af1924c

Browse files
committed
[Silver IV] Title: 카드 놓기, Time: 184 ms, Memory: 18076 KB -BaekjoonHub
1 parent 2c1f540 commit af1924c

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# [Silver IV] 카드 놓기 - 5568
2+
3+
[문제 링크](https://www.acmicpc.net/problem/5568)
4+
5+
### 성능 요약
6+
7+
메모리: 18076 KB, 시간: 184 ms
8+
9+
### 분류
10+
11+
자료 구조, 브루트포스 알고리즘, 집합과 맵, 해시를 사용한 집합과 맵, 백트래킹
12+
13+
### 제출 일자
14+
15+
2026년 2월 24일 15:16:27
16+
17+
### 문제 설명
18+
19+
<p>상근이는 카드 n(4 ≤ n ≤ 10)장을 바닥에 나란히 놓고 놀고있다. 각 카드에는 1이상 99이하의 정수가 적혀져 있다. 상근이는 이 카드 중에서 k(2 ≤ k ≤ 4)장을 선택하고, 가로로 나란히 정수를 만들기로 했다. 상근이가 만들 수 있는 정수는 모두 몇 가지일까?</p>
20+
21+
<p>예를 들어, 카드가 5장 있고, 카드에 쓰여 있는 수가 1, 2, 3, 13, 21라고 하자. 여기서 3장을 선택해서 정수를 만들려고 한다. 2, 1, 13을 순서대로 나열하면 정수 2113을 만들 수 있다. 또, 21, 1, 3을 순서대로 나열하면 2113을 만들 수 있다. 이렇게 한 정수를 만드는 조합이 여러 가지 일 수 있다.</p>
22+
23+
<p>n장의 카드에 적힌 숫자가 주어졌을 때, 그 중에서 k개를 선택해서 만들 수 있는 정수의 개수를 구하는 프로그램을 작성하시오.</p>
24+
25+
### 입력
26+
27+
<p>첫째 줄에 n이, 둘째 줄에 k가 주어진다. 셋째 줄부터 n개 줄에는 카드에 적혀있는 수가 주어진다.</p>
28+
29+
### 출력
30+
31+
<p>첫째 줄에 상근이가 만들 수 있는 정수의 개수를 출력한다.</p>
32+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.util.*;
2+
3+
public class Main {
4+
5+
static int[] arr;
6+
static int[] output;
7+
static boolean[] visited;
8+
static Set<Integer> cardCombinations = new HashSet<>();
9+
static StringBuilder sb = new StringBuilder();
10+
11+
public static void main(String[] args) {
12+
Scanner sc = new Scanner(System.in);
13+
14+
int n = sc.nextInt();
15+
int k = sc.nextInt();
16+
17+
arr = new int[n];
18+
output = new int[k];
19+
visited = new boolean[n];
20+
21+
for (int i = 0; i < n; i++) {
22+
arr[i] = sc.nextInt();
23+
}
24+
25+
permutation(0, n, k);
26+
System.out.println(cardCombinations.size());
27+
}
28+
29+
private static void permutation(int depth, int n, int r) {
30+
if (depth == r) {
31+
sb.setLength(0);
32+
for (int i = 0; i < r; i++) {
33+
sb.append(output[i]);
34+
}
35+
36+
cardCombinations.add(Integer.parseInt(sb.toString()));
37+
return;
38+
}
39+
40+
for (int i = 0; i < n; i++) {
41+
if (!visited[i]) {
42+
visited[i] = true;
43+
output[depth] = arr[i];
44+
permutation(depth + 1, n, r);
45+
visited[i] = false;
46+
}
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)