https://www.acmicpc.net/problem/15649
/*boj s3 15649 N과 M (1)*/
#include <iostream>
#define MAXN 9
using namespace std;
int N, M;
int answer[MAXN];
bool isUsed[MAXN];
void func(int k) {
if (k == M) {
for (int i = 0; i < M; i++) {
cout << answer[i] << " ";
}
cout << '\n';
return;
}
for (int i = 1; i <= N; i++) {
if (isUsed[i]) continue;
isUsed[i] = true;
answer[k] = i;
func(k + 1);
isUsed[i] = false;
}
}
int main(void) {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N >> M;
func(0);
}
next_permutation 쓴 경우
do {
순열 출력
} while(next_permutation(arr, arr+n);
/*s3 15649 N과M(1)_next_perm*/
#include <bits/stdc++.h>
#define MAX 10
using namespace std;
int n, m;
int arr[MAX];
int fac(int n) {
if (n == 0) return 1;
if (n == 1) return 1;
return n * fac(n - 1);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++) {
arr[i] = i + 1;
}
int alt = fac(n - m) - 1; // 건너뛸 개수
int cnt = alt;
do {
if (cnt == 0) {
for (int i = 0; i < m; i++) {
cout << arr[i] << " ";
}
cout << "\n";
cnt = alt;
continue;
}
cnt--;
} while (next_permutation(arr, arr + n));
}
'알고리즘' 카테고리의 다른 글
백준 s2 1182 부분수열의 합 c++ (0) | 2024.01.15 |
---|---|
백준 g4 9663 N-Queen c++ (0) | 2024.01.15 |
백준 g5 2448 별 찍기 - 11 c++ (0) | 2024.01.13 |
백준 2447 g5 별 찍기 - 10 c++ (0) | 2024.01.13 |
백준 s1 1074 Z c++ (0) | 2024.01.13 |