用C语言实现计算如下问题:公鸡5文钱一只,母鸡3文钱一只,小鸡3只一文钱,用100文钱买一百只鸡,其中公鸡,母鸡,小鸡都必须要有,问公鸡,母鸡,小鸡要买多少只刚好凑足100文钱。
时间: 2024-04-23 11:21:38 浏览: 54
这道题可以使用穷举法求解,即枚举所有可能的买法,找到符合条件的就输出结果。
具体实现如下:
```c
#include <stdio.h>
int main()
{
int x, y, z; // 公鸡、母鸡、小鸡的数量
int count = 0; // 符合条件的方案数
for (x = 1; x <= 20; x++) // 最多买20只公鸡
{
for (y = 1; y <= 33; y++) // 最多买33只母鸡
{
z = 100 - x - y; // 计算小鸡的数量
if (z % 3 == 0 && 5 * x + 3 * y + z / 3 == 100) // 符合条件
{
printf("公鸡:%d只,母鸡:%d只,小鸡:%d只\n", x, y, z);
count++;
}
}
}
printf("共%d种买法。\n", count);
return 0;
}
```
输出结果如下:
```
公鸡:4只,母鸡:18只,小鸡:78只
公鸡:8只,母鸡:11只,小鸡:81只
公鸡:12只,母鸡:4只,小鸡:84只
共3种买法。
```
因此,公鸡要买4只,母鸡要买18只,小鸡要买78只。
相关问题
用c语言编程编程实现百钱买百鸡问题:公鸡五文钱一只,母鸡三文钱一只,鸡仔一文钱三只,用100文钱买100只鸡,共有多少种买法?
好的,这是一个经典的著名问题。下面是用C语言编程实现的代码:
```c
#include <stdio.h>
int main()
{
int x, y, z;
int count = 0;
for(x = 0; x <= 20; x++) // 公鸡最多买20只
{
for(y = 0; y <= 33; y++) // 母鸡最多买33只
{
z = 100 - x - y; // 鸡仔的数量
if((z % 3 == 0) && (5 * x + 3 * y + z / 3 == 100)) // 鸡的数量和总价都满足条件
{
count++;
printf("第%d种买法:公鸡%d只,母鸡%d只,鸡仔%d只\n", count, x, y, z);
}
}
}
return 0;
}
```
运行结果为:
```
第1种买法:公鸡0只,母鸡25只,鸡仔75只
第2种买法:公鸡4只,母鸡18只,鸡仔78只
第3种买法:公鸡8只,母鸡11只,鸡仔81只
第4种买法:公鸡12只,母鸡4只,鸡仔84只
```
因此,用100文钱买100只鸡,共有4种买法。
用C语言写出百钱买百鸡: 一只公鸡5钱, 一只母鸡3钱,小鸡1钱3只。今有百钱买鸡百只 问公鸡、 母鸡、小鸡各几只
百钱买百鸡问题可以使用穷举法解决,具体思路和上一个问题一样。以下是用C语言实现百钱买百鸡问题的程序:
```c
#include <stdio.h>
int main() {
int x, y, z;
for (x = 0; x <= 20; x++) { // 公鸡数量不超过20只
for (y = 0; y <= 33; y++) { // 母鸡数量不超过33只
z = 100 - x - y; // 小鸡数量由鸡的总数量减去公鸡和母鸡的数量得到
if (5 * x + 3 * y + z / 3 == 100 && z % 3 == 0 && x + y + z == 100) { // 判断鸡的总价值是否为100,小鸡数量是否为3的倍数,鸡的总数量是否为100
printf("公鸡:%d只,母鸡:%d只,小鸡:%d只\n", x, y, z);
}
}
}
return 0;
}
```
运行程序后,输出结果为:
```
公鸡:0只,母鸡:25只,小鸡:75只
公鸡:4只,母鸡:18只,小鸡:78只
公鸡:8只,母鸡:11只,小鸡:81只
公鸡:12只,母鸡:4只,小鸡:84只
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)