题目描述:X星球要派出一个5人组成的观察团前往W星。
其中:
A国最多可以派出4人。
B国最多可以派出2人。
C国最多可以派出2人。
D国最多可以派出1人。
E国最多可以派出1人。
F国最多可以派出3人。
其中:
A国最多可以派出4人。
B国最多可以派出2人。
C国最多可以派出2人。
D国最多可以派出1人。
E国最多可以派出1人。
F国最多可以派出3人。
那么最终派往W星的观察团会有多少种国别的不同组合呢?
代码:
#include<stdio.h>
int count = 0;
void print(int x[])
{
for(int i = 0 ; i < 6 ; i++)printf("%d ",x[i]);
printf("\n");
}
void f(int x[],int data[],int loc,int goal)
{
if(loc == 6)
{
if(goal == 0){print(x);count++;}
return;
}
for(int i = 0 ; i <= (data[loc] < goal?data[loc]:goal) ; i++)
{
x[loc] = i;
f(x,data,loc + 1,goal - i);
}
}
int main()
{
int x[6] = {0};//每个国家派出的人数
int data[6] = {4,2,2,1,1,3};//每个国家最多派出的人数
printf("A B C D E F \n");
f(x,data,0,5);
printf("共有%d种派遣方法\n",count);
}
运行结果: