#include<stdio.h>
int main()
{
double consumption,cost;
scanf("%lf",&consumption);
if(0 <= consumption <= 50) {
cost = 0.53 * consumption;
printf("cost = %.2lf", cost);
}
else if(consumption > 50) {
cost = 0.53 * 50 + (consumption - 50) * (0.53 + 0.05);
printf("cost = %.2lf", cost);
}
else{
printf("Invalid Value!");
}
return 0;
}
其中 if(0 <= consumption <= 50)有误,当我输入100时,返回值为cost = 53.00 ,只判断了0 <= consumption
当我修改这条语句if(consumption <= 50 && consumption >= 0),返回值为cost = 55.50,符合题目要求。
所以还是要规范条件语句。