문제: 15656번: N과 M (7)
basic-algo-lecture/workbook/0x0C.md at master · encrypted-def/basic-algo-lecture
basic-algo-lecture/workbook/0x0C.md at master · encrypted-def/basic-algo-lecture
바킹독의 실전 알고리즘 강의 자료. Contribute to encrypted-def/basic-algo-lecture development by creating an account on GitHub.
github.com
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <iomanip> // setprecision을 사용하기 위한 헤더
#include <climits>
#include <list>
using namespace std;
int n, m;
int curNums[8]; // max 가 8임
void Funct(int functM, vector<int>& nums) {
if (functM == m) {
for (int i = 0; i < m; i++)
cout << curNums[i] << ' ';
cout << "\n";
return;
}
for (int i = 0; i < n; i++) {
curNums[functM] = nums[i];
Funct(functM + 1, nums);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
vector<int> nums(n);
for (int i = 0; i < n; i++) {
cin >> nums[i];
}
sort(nums.begin(), nums.end()); // 오름차순 정렬
Funct(0, nums);
return 0;
}
흠.. 중복을 허용하니까 방문 처리를 안 해도 된다. 수열은 사전순으로 출력해야 하므로 main 에서 nums 를 오름차순으로 정렬해줬다.
참고자료:
BaaaaaaaarkingDog | [실전 알고리즘] 0x0C강 - 백트래킹
[실전 알고리즘] 0x0C강 - 백트래킹
이번에는 백트래킹을 배워보도록 하겠습니다. 백트래킹도 재귀와 더불어 많은 사람들이 고통을 호소하는 알고리즘 중 하나이지만 의외로 그 재귀적인 구조에 매료되어서 참재미를 알아버리는
blog.encrypted.gg