Skip to content

Commit 87dc4e3

Browse files
committed
[Silver II] Title: N과 M (12), Time: 124 ms, Memory: 14480 KB -BaekjoonHub
1 parent 3e0b024 commit 87dc4e3

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
5+
public class Main {
6+
static int N,M,arr[];
7+
static int printArr[];
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+
arr = new int[N];
17+
printArr = new int[N];
18+
19+
st = new StringTokenizer(br.readLine());
20+
for(int i =0; i<N; i++) {
21+
arr[i] = Integer.parseInt(st.nextToken());
22+
}
23+
Arrays.sort(arr);
24+
25+
back(0,0);
26+
27+
System.out.println(sb);
28+
}
29+
30+
static void back(int depth, int index) {
31+
if(depth == M) {
32+
for(int i =0; i<M; i++) {
33+
sb.append(printArr[i]).append(" ");
34+
35+
}
36+
sb.append("\n");
37+
return;
38+
}
39+
int before = -1;
40+
for(int i = index; i<N; i++) {
41+
int now = arr[i];
42+
if(before != now) {
43+
printArr[depth] = arr[i];
44+
before = now;
45+
back(depth+1, i);
46+
}
47+
}
48+
49+
50+
}
51+
}
52+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# [Silver II] N과 M (12) - 15666
2+
3+
[문제 링크](https://www.acmicpc.net/problem/15666)
4+
5+
### 성능 요약
6+
7+
메모리: 14480 KB, 시간: 124 ms
8+
9+
### 분류
10+
11+
백트래킹
12+
13+
### 제출 일자
14+
15+
2024년 12월 30일 11:25:32
16+
17+
### 문제 설명
18+
19+
<p>N개의 자연수와 자연수 M이 주어졌을 때, 아래 조건을 만족하는 길이가 M인 수열을 모두 구하는 프로그램을 작성하시오.</p>
20+
21+
<ul>
22+
<li>N개의 자연수 중에서 M개를 고른 수열</li>
23+
<li>같은 수를 여러 번 골라도 된다.</li>
24+
<li>고른 수열은 비내림차순이어야 한다.
25+
<ul>
26+
<li>길이가 K인 수열 A가 A<sub>1</sub> ≤ A<sub>2</sub> ≤ ... ≤ A<sub>K-1</sub> ≤ A<sub>K</sub>를 만족하면, 비내림차순이라고 한다.</li>
27+
</ul>
28+
</li>
29+
</ul>
30+
31+
### 입력
32+
33+
<p>첫째 줄에 N과 M이 주어진다. (1 ≤ M ≤ N ≤ 8)</p>
34+
35+
<p>둘째 줄에 N개의 수가 주어진다. 입력으로 주어지는 수는 10,000보다 작거나 같은 자연수이다.</p>
36+
37+
### 출력
38+
39+
<p>한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다.</p>
40+
41+
<p>수열은 사전 순으로 증가하는 순서로 출력해야 한다.</p>
42+

0 commit comments

Comments
 (0)