一:题目描述
2013年起,火车票退票费比例下调:票面乘车站开车时间前48小时以上的按票价5%计退票费。同时,车票退票费的计算方法不再四舍五入到元为单位,而是以5角为单位:尾数小于0.25元的舍去、不小于0.25元且小于0.75元的计为0.5元、不小于0.75元的进为1元。编写一个函数,计算退票费,假定退票时间距离开车时间在48小时以上。函数原型如下:
double CancelFee(double price);
二:输入
输入一个实数,表示火车票票面价格。
三:输出
输出一个实数,表示退票费,结果保留一位小数。
四:样例输入
106
五:样例输出
5.5
六代码
#include<stdio.h>
double CancelFee(double price)
{
double x = price * 0.05;//计算本应该退的票费
int y = (int)x; // y等于退票费的整数部分//将x强制转换为int类型便会得到整数部分
double c = x - y;// c等于退票费的小数部分
if (c < 0.25)
{
return y;
}
else if (c >= 0.25 && c <= 0.75)
{
return x - c + 0.5;
}
else
{
return x - c + 1;
}
}
int main()
{
double price = 0;//票价初始化
scanf("%lf", &price);
double cancelfee = CancelFee(price);printf("%.1lf", cancelfee);
return 0;
}