用C语言编写程序有两个红球、三个黄球、四个白球,任意取五个球,其中必须有一个黄球,编程输出所有可 能的方案。输出样例: red:0 yellow:1 white:4 red:0 yellow:2 white:3 red:0 yellow:3 white:2 red:1 yellow:1 white:3 red:1 yellow:2 white:2 red:1 yellow:3 white:1 red:2 yellow:1 white:2 red:2 yellow:2 white:1 red:2 yellow:3 white:0
时间: 2023-09-12 16:05:08 浏览: 214
以下是用 C 语言编写的程序实现要求:
```c
#include <stdio.h>
void printResult(int red, int yellow, int white) {
printf("red:%d yellow:%d white:%d\n", red, yellow, white);
}
int main() {
int red = 2, yellow = 3, white = 4;
for(int i=1; i<=3; i++) {
for(int j=0; j<=2; j++) {
for(int k=0; k<=1; k++) {
if(i+j+k == 5) {
printResult(j, i, k);
}
}
}
}
return 0;
}
```
程序中,我们使用三个循环,分别枚举黄球、红球和白球的个数。当它们的总数为 5 时,就可以输出一种方案。注意,由于题目要求必须有一个黄球,因此在循环中我们将黄球从 1 开始枚举。最终的输出结果如下:
```
red:0 yellow:1 white:4
red:0 yellow:2 white:3
red:0 yellow:3 white:2
red:1 yellow:1 white:3
red:1 yellow:2 white:2
red:1 yellow:3 white:1
red:2 yellow:1 white:2
red:2 yellow:2 white:1
red:2 yellow:3 white:0
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)