出租车费
题目链接:http://codeup.cn/problem.php?cid=100000584&pid=1
思路
- 4公里以内,总价10,单价相当于2.5
- 4-8公里,单价2
- 8公里以上,单价2.4
- 看起来第2段最便宜,所以尽可能把车程控制在第2阶段,其次是第3阶段,这就是目标!
- n<4时,10元
- n>=4 && n<=8时,1和2阶段混合
- 当n减去超出8的部分还在4以内,用第3阶段,超过4不超过8,用第2阶段,超过8,就继续减8,当然对于的基本花费还是要加起来
代码
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
int main() {
double n;
while (scanf("%lf",&n),n!=0) {
double cost=0;
if(n<4)
cost=10;
if(n>=4 && n<=8)
cost=10+ceil(n-4)*2; // 不足一公里,按一公里算,用ceil
if(n>8) {
while(n>=8) {
cost+=18;
n-=8;
}
if(n<=4) {
cost+=ceil(n)*2.4;
} else {
cost+=10+ceil(n-4)*2;
}
}
if(cost==(int)cost)
printf("%d\n",(int)cost);
else
printf("%.1lf\n",cost);
}
return 0;
}