15649번: N과 M (1) (acmicpc.net)
15649번: N과 M (1)
한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해
www.acmicpc.net
#include <iostream>
#include <string>
using namespace std;
bool used[9] = { 0, };
void go(int index, int n, int m, string num) {
if (index == m) cout << num << '\n';
for (int i = 1; i <= n; i++) {
if (used[i]) continue;
used[i] = true;
go(index + 1, n, m, num + to_string(i) + " ");
used[i] = false;
}
}
int main() {
int N, M; cin >> N >> M;
go(0, N, M, "");
return 0;
}
'CodingTest > 백준' 카테고리의 다른 글
[c++] 백준 - 10971: 외판원 순회2 (0) | 2023.07.30 |
---|---|
[c++] 백준 - 1759번: 암호 만들기 | 재귀함수 (0) | 2023.07.20 |
[c++] 백준 - 1259번: 팰린드롬수 (0) | 2023.05.31 |
[c++] 백준 - 2798번: 블랙잭 (0) | 2023.05.31 |
[c++] 백준 - 1978: 소수 찾기 (0) | 2023.05.29 |