Skip to content

Commit b7cd2ad

Browse files
committed
[Silver III] Title: N과 M (1), Time: 256 ms, Memory: 22560 KB -BaekjoonHub
1 parent 9b3be15 commit b7cd2ad

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import java.util.*;
2+
import java.io.*;
3+
4+
public class Main {
5+
static int N, M;
6+
static int[] arr;
7+
static boolean[] isUsed;
8+
static StringBuilder sb = new StringBuilder();
9+
10+
public static void main(String[] args) throws IOException{
11+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
12+
StringTokenizer st = new StringTokenizer(br.readLine());
13+
14+
N = Integer.parseInt(st.nextToken());
15+
M = Integer.parseInt(st.nextToken());
16+
17+
arr = new int[N];
18+
isUsed = new boolean[N+1];
19+
20+
dfs(0);
21+
System.out.print(sb);
22+
23+
}
24+
25+
static void dfs(int depth){
26+
if(depth == M){ // 수열 길이 만족시 현재까지 완성된 수열 출력
27+
for(int i=0; i<M; i++){
28+
sb.append(arr[i]).append(" ");
29+
}
30+
sb.append("\n");
31+
return;
32+
}
33+
34+
for(int i=1; i<=N; i++){
35+
if(!isUsed[i]){
36+
isUsed[i] = true;
37+
arr[depth] = i; //현재 수열에 추가
38+
dfs(depth+1);
39+
isUsed[i] = false;
40+
}
41+
}
42+
}
43+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# [Silver III] N과 M (1) - 15649
2+
3+
[문제 링크](https://www.acmicpc.net/problem/15649)
4+
5+
### 성능 요약
6+
7+
메모리: 22560 KB, 시간: 256 ms
8+
9+
### 분류
10+
11+
백트래킹
12+
13+
### 제출 일자
14+
15+
2025년 11월 2일 03:21:28
16+
17+
### 문제 설명
18+
19+
<p>자연수 N과 M이 주어졌을 때, 아래 조건을 만족하는 길이가 M인 수열을 모두 구하는 프로그램을 작성하시오.</p>
20+
21+
<ul>
22+
<li>1부터 N까지 자연수 중에서 중복 없이 M개를 고른 수열</li>
23+
</ul>
24+
25+
### 입력
26+
27+
<p>첫째 줄에 자연수 N과 M이 주어진다. (1 ≤ M ≤ N ≤ 8)</p>
28+
29+
### 출력
30+
31+
<p>한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다.</p>
32+
33+
<p>수열은 사전 순으로 증가하는 순서로 출력해야 한다.</p>
34+

0 commit comments

Comments
 (0)