https://www.acmicpc.net/problem/15666
#include <iostream>
#include <ios>
#include <vector>
#include <algorithm>
using namespace std;
int N, M, numbers[8];
vector<int> series;
void dfs(int idx, int cnt);
void print();
int main() {
cin.tie(0); ios::sync_with_stdio(0);
cin >> N >> M;
for (int i = 0; i < N; i++) {
cin >> numbers[i];
}
sort(numbers, numbers + N);
dfs(0, 0);
return 0;
}
void dfs(int idx, int cnt) {
if (cnt == M) {
print();
return;
}
for (int i = idx; i < N; i++) {
series.push_back(numbers[i]);
dfs(i, cnt + 1);
series.pop_back();
int repeat = 0, repeatIdx = 1;
while (numbers[i] == numbers[i + repeatIdx]) {
repeat++; repeatIdx++;
}
i += repeat;
}
}
void print() {
for (int i = 0; i < M; i++) {
cout << series[i] << ' ';
}
cout << "\n";
}
'알고리즘 > BOJ' 카테고리의 다른 글
[BOJ] 2750. 수 정렬하기 (Bubble Sort) (0) | 2019.04.27 |
---|---|
[BOJ] 1934. 최소공배수 (0) | 2019.04.23 |
[BOJ] 15665. N과 M (11) (0) | 2019.04.21 |
[BOJ] 15664. N과 M (10) (0) | 2019.04.21 |
[BOJ] 15663. N과 M (9) (0) | 2019.04.20 |
댓글