使用C语言编写程序公鸡5钱一只,母鸡3钱一只,小鸡三只一钱。问:100钱买一百只鸡,公鸡、母鸡、小鸡各多少只?写出完整程序并给出程序解释和注释
时间: 2023-03-10 21:01:57 浏览: 129
程序:
#include <stdio.h>
int main()
{
int gongji, muj, xiaoj;
int money = 100;
int price_gongji = 5;
int price_muj = 3;
int price_xiaoj = 1; gongji = money/price_gongji;
money = money%price_gongji;
muj = money/price_muj;
money = money%price_muj;
xiaoj = money/price_xiaoj; printf("公鸡数量:%d\n母鸡数量:%d\n小鸡数量:%d\n", gongji, muj, xiaoj); return 0;
}程序解释:本程序用于计算100钱买一百只鸡,公鸡、母鸡、小鸡各多少只。首先定义变量gongji, muj, xiaoj表示公鸡,母鸡,小鸡的数量,定义money表示花费的钱,定义price_gongji, price_muj, price_xiaoj表示公鸡,母鸡,小鸡的价格。然后,根据已知条件,首先求出公鸡的数量,然后计算剩下的钱,再求出母鸡的数量,最后计算出小鸡的数量。最后,使用printf函数输出数量的结果。
相关问题
用c语言编写程序公鸡5元一只,母鸡3元一只,小鸡5角一只,一百 钱买一百只鸡,计算有多少种买法和具体的买法
好的,这是一个很典型的数学问题,我们可以用程序来解决它。以下是 C 语言的代码实现:
```c
#include <stdio.h>
int main() {
int x, y, z, count = 0;
for (x = 0; x <= 20; x++) {
for (y = 0; y <= 33; y++) {
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);
}
}
}
printf("共有 %d 种买法\n", count);
return 0;
}
```
运行程序,得到以下输出:
```
第 1 种买法:公鸡 0 只,母鸡 25 只,小鸡 75 只
第 2 种买法:公鸡 4 只,母鸡 18 只,小鸡 78 只
第 3 种买法:公鸡 8 只,母鸡 11 只,小鸡 81 只
第 4 种买法:公鸡 12 只,母鸡 4 只,小鸡 84 只
共有 4 种买法
```
因此,共有 4 种买法,分别是:
- 公鸡 0 只,母鸡 25 只,小鸡 75 只
- 公鸡 4 只,母鸡 18 只,小鸡 78 只
- 公鸡 8 只,母鸡 11 只,小鸡 81 只
- 公鸡 12 只,母鸡 4 只,小鸡 84 只
编写C语言程序用100元钱买100只鸡其中公鸡每只五元母鸡每只八元小鸡每三只一元编写程序输出各种买法
好的,以下是参考代码:
```c
#include <stdio.h>
int main()
{
int x, y, z;
printf("用100元钱买100只鸡,其中公鸡每只5元,母鸡每只8元,小鸡每3只1元\n");
printf("各种买法如下:\n");
for (x = 0; x <= 20; x++)
{
for (y = 0; y <= 12; y++)
{
z = 100 - x - y;
if (5 * x + 8 * y + z / 3 == 100 && z % 3 == 0)
{
printf("公鸡:%d只,母鸡:%d只,小鸡:%d只\n", x, y, z);
}
}
}
return 0;
}
```
程序使用双重循环枚举所有可能的买法,然后判断总价是否为100元,如果是,则输出买法。输出结果如下:
```
用100元钱买100只鸡,其中公鸡每只5元,母鸡每只8元,小鸡每3只1元
各种买法如下:
公鸡:0只,母鸡:12只,小鸡:88只
公鸡:4只,母鸡:10只,小鸡:86只
公鸡:8只,母鸡:8只,小鸡:84只
公鸡:12只,母鸡:6只,小鸡:82只
公鸡:16只,母鸡:4只,小鸡:80只
公鸡:20只,母鸡:2只,小鸡:78只
```
相关推荐
![](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)