2.借书方案知多少
小明有5本行书,要借给A, B, C这三位小朋友,若每人每次只能借1本,则可以有多少种不同的借法?
#include <stdio.h>
int main (void)
{
int a, b, c;
int count = 0;
for(a = 0; a < 5; a++){
for(b = 0; b < 5; b++){
for(c = 0; c < 5; c++){
if(a != b && a != c){
printf("A:%2d B:%2d C:%2d ", a, b, c);
count++;
if(count % 4 == 0){
printf("\n");
}
}
}
}
}
printf("共有%d中方法\n", count);
return 0;
}
改进:
#include <stdio.h>
int main (void)
{
int a, b, c;
int count = 0;
for(a = 0; a < 5; a++){
for(b = 0; b < 5; b++){
for(c = 0; c < 5&&a != b; c++){
if(a != b && a != c){
printf("A:%2d B:%2d C:%2d ", a, b, c);
count++;
if(count % 4 == 0){
printf("\n");
}
}
}
}
}
printf("共有%d中方法\n", count);
return 0;
}