728x90
반응형
#include <iostream>
#include <vector>
using namespace std;
void check(int N, int M, int start, vector<int> &comb)
{
if (comb.size() == M)
{
for (int c : comb)
cout << c << " ";
cout << '\n';
return;
}
for (int i = start; i <= N; i++)
{
comb.push_back(i);
check(N, M, i + 1, comb);
comb.pop_back();
}
}
int main()
{
int N, M;
cin >> N >> M;
vector<int> combination;
check(N, M, 1, combination);
return 0;
}
15652. N과 M(4)
초기 숫자부터 다시 시작하도록 i+1에서 i로 바꾸면 된다
#include <iostream>
#include <vector>
using namespace std;
void check(int N, int M, int start, vector<int> &comb)
{
if (comb.size() == M)
{
for (int c : comb)
cout << c << " ";
cout << '\n';
return;
}
for (int i = start; i <= N; i++)
{
comb.push_back(i);
check(N, M, i, comb);
comb.pop_back();
}
}
int main()
{
int N, M;
cin >> N >> M;
vector<int> combination;
check(N, M, 1, combination);
return 0;
}
728x90
반응형
'Baekjoon > Silver' 카테고리의 다른 글
Baekjoon 2805.EKO(나무자르기) c++ [Silver II] (0) | 2025.02.04 |
---|---|
Baekjoon 2003.수들의 합2 c++ [Silver IV] (0) | 2025.02.04 |
Baekjoon 10845.큐 c++ [Silver IV] (0) | 2024.12.30 |
Baekjoon 10816.숫자 카드 2 c++ [Silver IV] (0) | 2024.12.30 |
Baekjoon 1436.영화감독 슘 c++ [Silver V] (0) | 2024.12.28 |