从3个红球、6个白球和7个黑球中,任意取出8个球作为一组输出,在每组中,可以没有黑球,但必须要有红球和白球
求总的组数以及每组的红球、白球、黑球的数目。
思路:红球取值范围为1个到3个,白球为1个到6个,黑球为0个到7个
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include
<
stdio.h
>
void main()
{
int i,j,k,count = 0 ; /* 分别表示红球,白球,黑球的数目,以及总的组数 */
clrscr(); /* 清屏函数 */
for (i = 1 ;i <= 3 ;i ++ ) /* 红球的范围至少一个,最多有3个 */
{
for (j = 1 ;j <= 6 ;j ++ ) /* 白球的范围至少一个,最多6个 */
{
k = 8 - i - j;
if (k >= 0 && k <= 7 ) /* 可以没有黑球,至多有7个 */
{
count ++ ; /* 组数叠加 */
printf( " red ball:%d,white ball:%d,black ball:%d \n " ,i,j,k);
}
}
}
printf( " total count:%d " ,count);
}
void main()
{
int i,j,k,count = 0 ; /* 分别表示红球,白球,黑球的数目,以及总的组数 */
clrscr(); /* 清屏函数 */
for (i = 1 ;i <= 3 ;i ++ ) /* 红球的范围至少一个,最多有3个 */
{
for (j = 1 ;j <= 6 ;j ++ ) /* 白球的范围至少一个,最多6个 */
{
k = 8 - i - j;
if (k >= 0 && k <= 7 ) /* 可以没有黑球,至多有7个 */
{
count ++ ; /* 组数叠加 */
printf( " red ball:%d,white ball:%d,black ball:%d \n " ,i,j,k);
}
}
}
printf( " total count:%d " ,count);
}