#include<stdio.h>
#include<math.h>
//个位1 3 5 7是奇数
int main() {
int tel,a1,a2,a3,a4,t;
printf("请输入四位整数:\n");
scanf("%d",&tel);
a1=tel%10;
a2=tel/10%10;
a3=tel/100%10;
a4=tel/1000;
//加密
a1=(a1+5)%10;
a2=(a2+5)%10;
a3=(a3+5)%10;
a4=(a4+5)%10;
t=a1; a1=a4; a4=t;
t=a2; a2=a3; a3=t;
printf("加密后的数字:\n");
printf("%d%d%d%d\n",a1,a2,a3,a4);
return 0;
}
解决方案:
from math import ceil
# 普通出租车费用/公用计费器:
# 起步里程: 3 km, 起步费用: 10 元
# 超过 3 公里, 每公里: 2 元, 燃油附加费: 1 元
def taxi_cost(start, end):
dist_sum = abs(end - start)
if dist_sum <= 3:
return 10
total_fee = 10 + ceil((dist_sum - 3) / 1) * 2 + 1
return total_fee
此解决方案的核心是使用内置的 math 库中的 ceil()
方法,返回进一取整的结果。对于输入的参数,计算距离的绝对值,如果小于或等于 3 公里,直接返回起步费用 10 元;否则,计算超过 3 公里部分的里程除以 1 公里,进行进一取整,然后总费用加上燃油附加费 1 元和平时的起步费,得出总价格。
ceil()
方法在 Python 库中是常用的方法之一,因为它可用于支持大多数编程语言的进位存储和某些变量类型的整数除法。
假设输入的数据是这样的:
print(taxi_cost(0, 2)) # output: 10
print(taxi_cost(0, 4)) # output: 14
print(taxi_cost(2, 5)) # output: 13
输出为:
10
14
13
希望此回答能够帮助你解决问题。如果你需要其他帮助,请让我知道。
lc = int(input())
fee = 10
if lc < 0:
print("Input error")
elif lc < 3:
print(fee)
elif lc < 10:
fee += (lc - 3) * 2
print(fee)
else:
fee += 14 + (lc - 10) * 3
print(fee)
参考new Bing编写:
#include <stdio.h>
int main()
{
const int BASE_DISTANCE = 3; // 起步里程
const int BASE_PRICE = 10; // 起步费用
const double UNIT_PRICE_1 = 2.0; // 10公里内每公里单价
const double UNIT_PRICE_2 = 3.0; // 超过10公里后,每公里单价
const int CUTOFF_DISTANCE = 10; // 超过10公里开始加收回空补贴费
double distance, price;
scanf("%lf", &distance);
if (distance < 0) {
printf("Input error!\n");
return 0;
}
if (distance <= BASE_DISTANCE) {
price = BASE_PRICE;
} else if (distance <= CUTOFF_DISTANCE) {
price = BASE_PRICE + ((distance - BASE_DISTANCE) * UNIT_PRICE_1);
} else {
price = BASE_PRICE + (CUTOFF_DISTANCE - BASE_DISTANCE) * UNIT_PRICE_1
+ (distance - CUTOFF_DISTANCE) * UNIT_PRICE_2 * 1.5;
}
printf("%.0f\n", price + 0.5); // 四舍五入
return 0;
}