#include <stdio.h>
int cost(int distances, int times)
{
double money;
if (distances <= 3){
return 14;
}
else if (times<5 || times >= 23){
money = 1.2*(distances - 3)*2.3 + 14;
}
else{
money = (distances - 3)*2.3 + 14;
}
return money;
}
int main()
{
printf("打车总费用:%.1f", cost(12, 9) + cost(12, 18));
system("pause");
}
#include <stdio.h> float cost(float distances, int times) { float money; if (distances <= 3) { return 14; } else if (times < 5 || times >= 23) { money = 1.2*(ceil(distances) - 3)*2.3 + 14; } else { money = (ceil(distances) - 3)*2.3 + 14; } return money; } int main() { printf("打车总费用:%0.1f", cost(12, 9) + cost(12, 18)); system("pause"); return 0; }
之所以输出为零是因为这句“printf("打车总费用:%.1f", cost(12, 9) + cost(12, 18));”,你定义的cost函数是int型,你却用%0.1f去打印,所以你如果想输出带小数的可以将cost函数定义为float型或double型,这两个类型区别是长度不同,看需要选(这只是题外话,你这里不需要想那么多);
正常计费有个潜规则,向上取证,比如你打车做了4.5公里,正常计费会按5公里计算,不过你也可以不考虑,具体看情况。
能理解的话望采纳~