Problem Description
有编号分别为0,1,2,3,4的5本书,准备分给5个人A,B,C,D,E。假设5个人对5本书的阅读兴趣如下表所示。
写一个程序,输出所有的分书方案。
Input Description
无
Output Description
按照格式:num = n: a b c d e 输出
期中:n为方案号,从1开始;a, b, c, d, e为5个人A,B,C,D,E分到的书的编号。
#include <stdio.h>
#include <stdlib.h>
int n;
int interest[5][5] = {{0, 0, 1, 1, 0},
{1, 1, 0, 0, 1},
{0, 1, 1, 0, 1},
{0, 0, 0, 1, 0},
{0, 1, 0, 0, 1}};
int book[5]={0,0,0,0,0};
int person[5];
int divide_The_Book(int x){
for (int i = 0; i < 5; i++) {
if(interest[x][i]==1 && book[i]==0){
book[i]=x+1;
if(x==4){
n++;
for (int j = 0; j < 5; ++j) {
int temp = 0;
temp = book[j]-1;
person[temp]=j;
}
printf("num = %d:",n);
for (int k = 0; k < 5; k++) {
printf(" %d",person[k]);
}
printf("\n");
} else{
divide_The_Book(x+1);
}
book[i]=0;
}
}
}
int main(){
n=0;
divide_The_Book(0);
return 0;
};