有AABBBC六个字母,挑出三个,有哪些挑法?
#include<iostream>
using namespace std;
int minnum(int a, int b) {
return a > b ? b : a;
}
void work(int x[]) {
for (int i = 0; i < 3; i++) {
for (int k = 0; k < x[i]; k++) {
if (i == 0) {
cout << "A";
}
if (i == 1) {
cout << "B";
}
if (i == 2) {
cout << "C";
}
}
}
cout << endl;
}
//k为当前考虑的位置,goal为剩余的名额
void fun(int data[], int x[], int k, int goal) {
if (k == 3) {
if (goal == 0) {
work(x);
}
return;
}
for (int i = 0; i <= minnum(goal, data[k]); i++) {
x[k] = i;
fun(data, x, k + 1, goal - i);//递归出口在goal=0时,所以前面占了几个位置,goal就当减去几
x[k] = 0;
}
}
//用data来量化每个字母的个数,同样的,用x来量化最终方案。
int main() {
int data[] = { 2,3,1 };
int x[] = { 0,0,0 };
fun(data, x, 0, 3);
return 0;
}