알고리즘
백준 15651 N과 M (3)
중국고대사
2019. 4. 4. 16:13
앞선 1, 2번 문제와 별 차이가 없다.
모든 경우의 수를 다 출력하는 방법이다.
solve에 전달할 인자도 필요가 없다.
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() { //뽑아야 할 개수만큼 뽑고 나면 출력한다. if (v.size() == m) { for (auto x : v) { printf("%d ", x); } printf("\n"); return; } for (int i = 1; i <= n; i++) { //중복되면 안되므로 이미 들어있는 원소인지 확인한다. v.push_back(i); solve(); v.pop_back(); } } int main(int argc, char** argv) { cin >> n >> m; solve(); return 0; } | cs |