[C++] Permutation, Combination
2024. 8. 10. 01:27ㆍ🧑🏻💻/C & C++
#include <iostream>
#include <vector>
#define n 4
#define r 2
using namespace std;
vector<int> pArr(r, 0);
vector<bool> check(n + 1, false);
void printArray(vector<int> arr) {
for (int i = 0; i < arr.size(); i++) {
cout << arr[i] << " ";
}
cout << endl;
}
void permutation(int depth) {
if (depth == r) {
printArray(pArr);
return;
}
for (int i = 1; i <= n; i++) {
if (!check[i]) {
check[i] = true;
pArr[depth] = i;
permutation(depth + 1);
check[i] = false;
}
}
}
int main(void) {
cout << "순열 (순서o, 중복x)" << endl;
permutation(0);
return 0;
}
#include <iostream>
#include <vector>
#define n 4
#define r 2
using namespace std;
vector<int> pArr(r, 0);
void printArray(vector<int> arr) {
for (int i = 0; i < arr.size(); i++) {
cout << arr[i] << " ";
}
cout << endl;
}
void permutation(int depth) {
if (depth == r) {
printArray(pArr);
return;
}
for (int i = 1; i <= n; i++) {
pArr[depth] = i;
permutation(depth + 1);
}
}
int main(void) {
cout << "순열 (순서o, 중복o)" << endl;
permutation(0);
return 0;
}
#include <iostream>
#include <vector>
#define n 4
#define r 3
using namespace std;
vector<int> cArr(r, 0);
void printArray(vector<int> arr) {
for (int i = 0; i < arr.size(); i++) {
cout << arr[i] << " ";
}
cout << endl;
}
void combination(int depth, int next) {
if (depth == r) {
printArray(cArr);
return;
}
for (int i = next; i <= n; i++) {
cArr[depth] = i;
combination(depth + 1, i + 1);
}
}
int main(void) {
cout << "조합 (순서x, 중복x)" << endl;
combination(0, 1);
return 0;
}
#include <iostream>
#include <vector>
#define n 3
#define r 3
using namespace std;
vector<int> cArr(r, 0);
void printArray(vector<int> arr) {
for (int i = 0; i < arr.size(); i++) {
cout << arr[i] << " ";
}
cout << endl;
}
void combination(int depth, int next) {
if (depth == r) {
printArray(cArr);
return;
}
for (int i = next; i <= n; i++) {
cArr[depth] = i;
combination(depth + 1, i);
}
}
int main(void) {
cout << "조합 (순서x, 중복o)" << endl;
combination(0, 1);
return 0;
}
'🧑🏻💻 > C & C++' 카테고리의 다른 글
[C++] 순열, 조합 (0) | 2024.10.05 |
---|---|
[C++] vector (0) | 2024.09.28 |
[C] const (0) | 2022.09.15 |
[C] strjoin / strjoin.c / strjoin in c (0) | 2022.09.14 |
[C] substr / substr.c / substr in c (0) | 2022.09.14 |