算法笔记 codeup 出租车费

出租车费

题目链接: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;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值