#include <bits/stdc++.h>
using namespace std;
int output[10];
void go(int index, int selected, int max_number, int length) {
if (selected == length) {
for (int i = 0; i < length; i++) {
cout << output[i] << ' ';
}
cout << '\n';
return;
}
if (index > max_number) return;
output[selected] = index;
go(index+1, selected+1, max_number, length);
go(index+1, selected, max_number, length);
}
int main() {
int N, M;
cin >> N >> M;
go(1, 0, N, M);
}
위의 코드와 같이 재귀로 조합을 구현할 수 있다.
https://www.acmicpc.net/problem/15650
15650번: N과 M (2)
한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해
www.acmicpc.net
위의 문제의 정답 코드이기도 하다.
'Algorithm > 이론' 카테고리의 다른 글
배열로 Heap 구현 예시 (0) | 2022.09.11 |
---|---|
다익스트라 알고리즘 예시 코드 [C++, Python] (0) | 2022.08.25 |
배열 3개로 연결리스트 흉내내기 (0) | 2022.07.22 |
BFS 예시 코드 [C++, Python] (0) | 2022.07.09 |
재귀로 순열 구현하기 [C++] (0) | 2022.07.08 |