用100元买苹果、火龙果、芒果,总数为30个。若苹果3元一个,火龙果6元一个,芒果2元一个。问各种水果分别买多少个。输出所有可能的方案。c语言代码
时间: 2023-05-31 20:07:48 浏览: 216
8类水果图片数据集(每类100张图片左右)['橘子','菠萝','苹果','木瓜','火龙果','香蕉','樱桃','芒果']
5星 · 资源好评率100%
#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;
}
阅读全文