赞
踩
从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
- #include <stdio.h>
- #include <stdlib.h>
-
- int red,white,black;
- int Fun(void)
- {
- int sum=0;
- for(red=1;red<=3;red++)
- {
- for(white=1;white<=5;white++)
- {
- for(black=0;black<=6;black++)
- {
- if(red+white+black==8)
- {
- printf("red:%4d white:%4d black:%4d\n",red,white,black);
- sum++;
- }
- }
- }
- }
- return sum;
- }
-
-
- int main()
- {
- int sum;
- printf("The result:\n");
- sum=Fun();
- printf("sum=%4d\n",sum);
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。