可以運行但是結果錯誤- -,求大神幫忙看一下
#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");
}
2019-05-20
之所以輸出為零是因為這句“printf("打車總費用:%.1f", cost(12, 9) + cost(12, 18));”,你定義的cost函數是int型,你卻用%0.1f去打印,所以你如果想輸出帶小數的可以將cost函數定義為float型或double型,這兩個類型區別是長度不同,看需要選(這只是題外話,你這里不需要想那么多);
正常計費有個潛規則,向上取證,比如你打車做了4.5公里,正常計費會按5公里計算,不過你也可以不考慮,具體看情況。
能理解的話望采納~