Code/BOJ
순열, 조합 뽑기 연습 N과 M (1)~(12)
milkteagood
2020. 2. 9. 20:39
728x90
반응형
N과 M (1)
출처: https://www.acmicpc.net/problem/15649

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1부터 N까지 자연수 중에서 중복 없이 M개를 고른 수열 | |
#include <iostream> | |
#include <cstring> | |
#define endl '\n' | |
using namespace std; | |
int visit[9]; | |
int save[9]; | |
int n, k; | |
void dfs(int num, int cnt) { | |
if (cnt == k) { | |
for (int i = 0; i < k; i++) { | |
cout << save[i] << " "; | |
} | |
cout << endl; | |
return; | |
} | |
for (int i = 1; i <= n; i++) { | |
if (visit[i])continue; | |
visit[i] = true; | |
save[cnt] = i; | |
dfs(i, cnt + 1); | |
visit[i] = false; | |
} | |
} | |
int main() { | |
cin >> n >> k; | |
dfs(0, 0); | |
} |
N과 M (2)
출처: https://www.acmicpc.net/problem/15650

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define _CRT_SECURE_NO_WARNINGS | |
#include <stdio.h> | |
using namespace std; | |
int visit[9]; | |
int arr[9]; | |
int n, k; | |
void dfs(int cnt,int num) { | |
if (cnt == k) { | |
for (int i = 0; i < k; i++) { | |
printf("%d ", visit[i]); | |
} | |
printf("\n"); | |
return; | |
} | |
for (int i = num; i <= n; i++) { | |
if (visit[cnt])continue; | |
visit[cnt] = i; | |
dfs(cnt + 1,i+1); | |
visit[cnt] = 0; | |
} | |
} | |
int main() { | |
scanf("%d %d", &n, &k); | |
dfs(0,1); | |
} |
N과 M (3)
출처: https://www.acmicpc.net/problem/15649

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// #15651 N과 M(3) | |
#include <iostream> | |
#include <cstring> | |
#define endl '\n' | |
using namespace std; | |
int n, k; | |
int save[8]; | |
void dfs(int num, int cnt) { | |
if (cnt == k) { | |
for (int i = 0; i < k; i++) { | |
cout << save[i] << " "; | |
}cout << endl; | |
return; | |
} | |
for (int i = 1; i <= n; i++) { | |
save[cnt] = i; | |
dfs(i, cnt + 1); | |
} | |
} | |
int main() { | |
cin >> n >> k; | |
dfs(0, 0); | |
} |
N과 M (4)
출처:https://www.acmicpc.net/problem/15652

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// #15652 N과 M (4) | |
#include <iostream> | |
#define endl '\n' | |
using namespace std; | |
int n, k; | |
int save[9]; | |
void dfs(int num, int cnt) { | |
if (cnt == k) { | |
for (int i = 0; i < cnt; i++) { | |
cout << save[i] << " "; | |
}cout << endl; | |
return; | |
} | |
for (int i = num; i <= n; i++) { | |
save[cnt] = i; | |
dfs(i, cnt + 1); | |
} | |
} | |
int main() { | |
cin >> n >> k; | |
dfs(1, 0); | |
} |
N과 M (5)
출처:https://www.acmicpc.net/problem/15654

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// #15654 N과 M (5) | |
#include <iostream> | |
#include <cstring> | |
#include <algorithm> | |
#define endl '\n' | |
using namespace std; | |
int n, k; | |
int arr[9]; | |
int visit[9]; | |
int save[9]; | |
void dfs(int num, int cnt) { | |
if (cnt == k) { | |
for (int i = 0; i < k; i++) { | |
cout << save[i] << " "; | |
} | |
cout << endl; | |
return; | |
} | |
for (int i = 0; i < n; i++) { | |
if (visit[i])continue; | |
visit[i] = true; | |
save[cnt] = arr[i]; | |
dfs(i, cnt + 1); | |
visit[i] = false; | |
} | |
} | |
int main() { | |
cin >> n >> k; | |
for (int i = 0; i < n; i++) { | |
cin >> arr[i]; | |
} | |
sort(arr, arr + n); | |
dfs(0, 0); | |
} |
N과 M (6)
출처:https://www.acmicpc.net/problem/15655

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// #15655 N과 M (6) | |
#include <iostream> | |
#include <cstring> | |
#include <algorithm> | |
#define endl '\n' | |
using namespace std; | |
int n, k; | |
int arr[9]; | |
int visit[9]; | |
void dfs(int num, int cnt) { | |
if (cnt == k) { | |
for (int i = 0; i < n; i++) { | |
if (visit[i])cout << arr[i] << " "; | |
} | |
cout << endl; | |
return; | |
} | |
for (int i = num; i < n; i++) { | |
if (visit[i])continue; | |
visit[i] = true; | |
dfs(i, cnt + 1); | |
visit[i] = false; | |
} | |
} | |
int main() { | |
cin >> n >> k; | |
for (int i = 0; i < n; i++) { | |
cin >> arr[i]; | |
} | |
sort(arr, arr + n); | |
dfs(0, 0); | |
} |
N과 M (7)
출처:https://www.acmicpc.net/problem/15656

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// #15655 N과 M (7) | |
#include <iostream> | |
#include <cstring> | |
#include <algorithm> | |
#define endl '\n' | |
using namespace std; | |
int n, k; | |
int arr[9]; | |
int save[9]; | |
void dfs(int num, int cnt) { | |
if (cnt == k) { | |
for (int i = 0; i < k; i++) { | |
cout <<save[i] << " "; | |
} | |
cout << endl; | |
return; | |
} | |
for (int i = 0; i < n; i++) { | |
save[cnt] = arr[i]; | |
dfs(i, cnt + 1); | |
} | |
} | |
int main() { | |
cin >> n >> k; | |
for (int i = 0; i < n; i++) { | |
cin >> arr[i]; | |
} | |
sort(arr, arr + n); | |
dfs(0, 0); | |
} |
N과 M (8)
출처:https://www.acmicpc.net/problem/15657

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// #15655 N과 M (8) | |
#include <iostream> | |
#include <cstring> | |
#include <algorithm> | |
#define endl '\n' | |
using namespace std; | |
int n, k; | |
int arr[9]; | |
int save[9]; | |
void dfs(int num, int cnt) { | |
if (cnt == k) { | |
for (int i = 0; i < k; i++) { | |
cout << save[i] << " "; | |
} | |
cout << endl; | |
return; | |
} | |
for (int i = num; i < n; i++) { | |
save[cnt] = arr[i]; | |
dfs(i, cnt + 1); | |
} | |
} | |
int main() { | |
cin >> n >> k; | |
for (int i = 0; i < n; i++) { | |
cin >> arr[i]; | |
} | |
sort(arr, arr + n); | |
dfs(0, 0); | |
} |
N과 M (9)
출처:https://www.acmicpc.net/problem/15663

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// N과 M (9) | |
#include <iostream> | |
#include <cstring> | |
#include <algorithm> | |
using namespace std; | |
#define endl '\n' | |
int n, k; | |
int arr[9]; | |
int realvisit[9]; | |
int save[9]; | |
void dfs(int num, int cnt) { | |
if (cnt == k) { | |
for (int i = 0; i < k; i++) { | |
cout << save[i] << " "; | |
}cout << endl; | |
return; | |
} | |
int visit[10001]; | |
memset(visit, 0, sizeof(visit)); | |
for (int i = 0; i < n; i++) { | |
if (visit[arr[i]] || realvisit[i])continue; | |
visit[arr[i]] = true; | |
realvisit[i] = true; | |
save[cnt] = arr[i]; | |
dfs(i, cnt + 1); | |
realvisit[i] = false; | |
} | |
} | |
int main() { | |
cin >> n >> k; | |
for (int i = 0; i < n; i++) { | |
cin >> arr[i]; | |
} | |
sort(arr, arr + n); | |
int visit[10001]; | |
memset(visit, 0, sizeof(visit)); | |
for (int i = 0; i < n; i++) { | |
if (visit[arr[i]])continue; | |
visit[arr[i]] = true; | |
realvisit[i] = true; | |
save[0] = arr[i]; | |
dfs(i, 1); | |
realvisit[i] = false; | |
} | |
} |
N과 M (10)
출처:https://www.acmicpc.net/problem/15664

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// N과 M (10) | |
#include <iostream> | |
#include <cstring> | |
#include <algorithm> | |
using namespace std; | |
#define endl '\n' | |
int n, k; | |
int arr[9]; | |
int realvisit[9]; | |
int save[9]; | |
void dfs(int num, int cnt) { | |
if (cnt == k) { | |
for (int i = 0; i < n; i++) { | |
if (realvisit[i])cout << arr[i] << " "; | |
} | |
cout << endl; | |
return; | |
} | |
int visit[10001]; | |
memset(visit, 0, sizeof(visit)); | |
for (int i = num; i < n; i++) { | |
if (visit[arr[i]] || realvisit[i])continue; | |
visit[arr[i]] = true; | |
realvisit[i] = true; | |
dfs(i, cnt + 1); | |
realvisit[i] = false; | |
} | |
} | |
int main() { | |
cin >> n >> k; | |
for (int i = 0; i < n; i++) { | |
cin >> arr[i]; | |
} | |
sort(arr, arr + n); | |
dfs(0, 0); | |
} |
N과 M (11)
출처:https://www.acmicpc.net/problem/15665

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 15665 N과 M (11) | |
//N개의 자연수 중에서 M개를 고른 수열 | |
//같은 수를 여러 번 골라도 된다. | |
#include <iostream> | |
#include <cstring> | |
#include <algorithm> | |
using namespace std; | |
#define endl '\n' | |
int n, k; | |
int arr[9]; | |
int realvisit[9]; | |
int save[9]; | |
void dfs(int num, int cnt) { | |
if (cnt == k) { | |
for (int i = 0; i < cnt; i++) { | |
cout << save[i] << " "; | |
}cout << endl; | |
return; | |
} | |
int visit[10001]; | |
memset(visit, 0, sizeof(visit)); | |
for (int i = 0; i < n; i++) { | |
if (visit[arr[i]])continue; | |
visit[arr[i]] = true; | |
save[cnt] = arr[i]; | |
dfs(i, cnt + 1); | |
} | |
} | |
int main() { | |
cin >> n >> k; | |
for (int i = 0; i < n; i++) { | |
cin >> arr[i]; | |
} | |
sort(arr, arr + n); | |
int visit[10001]; | |
memset(visit, 0, sizeof(visit)); | |
for (int i = 0; i < n; i++) { | |
if (visit[arr[i]])continue; | |
visit[arr[i]] = true; | |
save[0] = arr[i]; | |
dfs(0, 1); | |
} | |
} |
N과 M (12)
출처:https://www.acmicpc.net/problem/15666

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 15666 N과 M (12) | |
//N개의 자연수 중에서 M개를 고른 수열 | |
//같은 수를 여러 번 골라도 된다. | |
//고른 수열은 비내림차순이어야 한다. | |
//길이가 K인 수열 A가 A1 ≤ A2 ≤ ... ≤ AK - 1 ≤ AK를 만족하면, 비내림차순이라고 한다. | |
#include <iostream> | |
#include <cstring> | |
#include <algorithm> | |
using namespace std; | |
#define endl '\n' | |
int n, k; | |
int arr[9]; | |
int realvisit[9]; | |
int save[9]; | |
void dfs(int num, int cnt) { | |
if (cnt == k) { | |
for (int i = 0; i < cnt; i++) { | |
cout << save[i] << " "; | |
}cout << endl; | |
return; | |
} | |
int visit[10001]; | |
memset(visit, 0, sizeof(visit)); | |
for (int i = num; i < n; i++) { | |
if (visit[arr[i]])continue; | |
visit[arr[i]] = true; | |
save[cnt] = arr[i]; | |
dfs(i, cnt + 1); | |
} | |
} | |
int main() { | |
cin >> n >> k; | |
for (int i = 0; i < n; i++) { | |
cin >> arr[i]; | |
} | |
sort(arr, arr + n); | |
int visit[10001]; | |
memset(visit, 0, sizeof(visit)); | |
for (int i = 0; i < n; i++) { | |
if (visit[arr[i]])continue; | |
visit[arr[i]] = true; | |
save[0] = arr[i]; | |
dfs(i, 1); | |
} | |
} |
728x90
반응형