三色球分组
从3个红球,5个白球,6个黑球中任意取出8个作为一组进行输出。在每组中可以没有黑球,但必须要有红球和白球。编程实现以上功能。用函数返回其组合数,在函数中打印每组的组合
函数原型为: int Fun (void);
程序运行结果示例:
The result:
red: 1 white: 1 black: 6
red: 1 white: 2 black: 5
red: 1 white: 3 black: 4
red: 1 white: 4 black: 3
red: 1 white: 5 black: 2
red: 2 white: 1 black: 5
red: 2 white: 2 black: 4
red: 2 white: 3 black: 3
red: 2 white: 4 black: 2
red: 2 white: 5 black: 1
red: 3 white: 1 black: 4
red: 3 white: 2 black: 3
red: 3 white: 3 black: 2
red: 3 white: 4 black: 1
red: 3 white: 5 black: 0
sum= 15
输入格式: 无
输出格式:
输出提示:"The result:\n"
输出格式:"red:%4d white:%4d black:%4d\n"
输出组合数格式:"sum=%4d\n"
代码如下:
#include <stdio.h>
int Fun(void);
int main()
{
int sum;
sum = Fun();
printf("sum=%4d\n", sum);
return 0;
}
int Fun(void)
{
int i, j, k, sum = 0;
printf("The result:\n");
for (i = 1; i <= 3; i++)
{
for (j = 1; j <= 5; j++)
{
for (k = 0; k <= 6; k++)
{
if (i + j + k == 8)
{
printf("red:%4d white:%4d black:%4d\n", i, j, k);
sum = sum + 1;
}
}
}
}
return sum;
}