알고리즘
백준 15650 N과 M (2)
중국고대사
2019. 4. 4. 15:59
N과 M (1) 문제에서 조금만 변형시키면 된다.
오름차순으로 출력해야 하므로, solve 안에서 for 문의 i는 인자인 num보다 1크게 하여 시작한다.
중복은 알아서 걸러지므로 삭제 시켜도 된다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
int n, m;
vector<int> v;
void solve(int num) {
//뽑아야 할 개수만큼 뽑고 나면 출력한다.
if (v.size() == m) {
for (auto x : v) {
printf("%d ", x);
}
printf("\n");
return;
}
for (int i = num + 1; i <= n; i++) {
//중복되면 안되므로 이미 들어있는 원소인지 확인한다.
v.push_back(i);
solve(i);
v.pop_back();
}
}
int main(int argc, char** argv) {
cin >> n >> m;
solve(0);
return 0;
}
|
cs |