Life Engineering
[BOJ 15654] N과 M(5) (C++) 본문
https://www.acmicpc.net/problem/15654
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
vector<int> v;
vector<vector<int>> ans;
int N, M, x;
bool check[9] = { false, };
void dfs(int cnt, vector<int> &c) {
if (cnt == M) {
ans.push_back(c);
return;
}
for (int i = 0; i < N; i++) {
if (!check[i]) {
c.push_back(v[i]);
check[i] = true;
dfs(cnt + 1, c);
check[i] = false;
c.pop_back();
}
}
}
int main() {
cin >> N >> M;
vector<int> temp;
for (int i = 0; i < N; i++) {
cin >> x;
v.push_back(x);
}
sort(v.begin(), v.end());
dfs(0, temp);
for (int i = 0; i < ans.size(); i++) {
for (int j = 0; j < M; j++) {
cout << ans[i][j] << " ";
}
cout << "\n";
}
return 0;
}
vector를 파라미터로 전달하는 것보다는 더 간단한 방법은 배열을 하나 만들어서, cnt를 인덱스로 삼아 거기다가 숫자를 넣어줘서 cnt==M 이면 그 배열을 출력하는 것이다. 그러면 자동 갱신될 수 있다!
'Problem Solving' 카테고리의 다른 글
[프로그래머스] 순위 (C++) (0) | 2021.11.05 |
---|---|
[프로그래머스] 순위 검색 (C++) (0) | 2021.11.04 |
[프로그래머스] 프린터 (C++) (0) | 2021.10.27 |
[BOJ 21609] 상어 중학교 (C++) (0) | 2021.10.21 |
[프로그래머스] 가장 큰 수 (C++) (0) | 2021.10.21 |