用100元买苹果、火龙果、芒果,总数为30个。若苹果3元一个,火龙果6元一个,芒果2元一个。问各种水果分别买多少个。输出所有可能的方案。c语言代码
时间: 2023-05-31 15:07:48 浏览: 84
#include <stdio.h>
int main() {
int apple_price = 3, mango_price = 2, dragonfruit_price = 6;
int total_fruit = 30, total_money = 100;
int apple, mango, dragonfruit, solution_count = 0;
for (apple = 0; apple <= total_fruit; apple++) {
for (mango = 0; mango <= total_fruit - apple; mango++) {
dragonfruit = total_fruit - apple - mango; // 确定火龙果的数量
int total_spent = apple * apple_price +
mango * mango_price +
dragonfruit * dragonfruit_price;
if (total_spent == total_money) {
// 输出一个解决方案
printf("Solution %d: Apple: %d, Mango: %d, Dragonfruit: %d\n",
solution_count + 1, apple, mango, dragonfruit);
solution_count++;
}
}
}
if (solution_count == 0) {
printf("No solution found.\n");
}
return 0;
}
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.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)