2、百元买鸡问题:已知公鸡5元一只,母鸡3元一只,小鸡1元三只,用100元买100只鸡,问公鸡、母鸡、小鸡各多少只?请写出多种完整c语言代码
时间: 2023-03-14 17:03:17 浏览: 312
答:要用100元买100只鸡,可以买20只公鸡(20×5=100),买15只母鸡(15×3=45),以及65只小鸡(65×3=195)。以下是用C语言实现此题的代码:int main(){
int male_chicken = 0;
int female_chicken = 0;
int small_chicken = 0;
int total_price = 100;
int temp_price = 0; while (temp_price!=total_price){
if(temp_price + 5 <= total_price){
male_chicken++;
temp_price = temp_price + 5;
}
else if(temp_price + 3 <= total_price){
female_chicken++;
temp_price = temp_price + 3;
}
else{
small_chicken++;
temp_price = temp_price + 1;
}
}printf("Male chicken: %d\n", male_chicken);
printf("Female chicken: %d\n", female_chicken);
printf("Small chicken: %d\n", small_chicken);return 0;
}
阅读全文