PAT 2.出租车计价 python实现

2022-08-01,,,,

本题要求编写程序,计算4个整数的和与平均值。题目保证输入与输出均在整型范围内。

输入格式:

输入在一行中给出4个整数,其间以空格分隔。

输出格式:

在一行中按照格式“Sum = 和; Average = 平均值”顺序输出和与平均值,其中平均值精确到小数点后一位。

输入样例:

1 2 3 4

输出样例:

Sum = 10; Average = 2.5

参考代码

import math

num_str = input()
li_str = num_str.split()
fare = 10 #里程数费用
count = 0 #总费用
waitCost = 0 #等待花费
km = round(eval(li_str[0]),1) #保留一位小数
waitTime = round(eval(li_str[1]),0) #不保留小数

#计算历程费用
if km > 3 and km <= 10:
    fare = fare + (km-3)*2
elif km>10:
    fare = fare + (10-3)*2 +(km-10)*3

#计算等待花费
if waitTime>=5:
    waitCost = math.floor(waitTime/5)*2

count = round(fare + waitCost)
print(count)

思路:

  1. 接收键盘录入数据,且利用split()方法存入列表。
  2. 初始化各个变量。
  3. 简单处理里程和等待时间的输入数据,利用round()函数四舍五入保留小数。
  4. 利用分支语句计算里程费用和等待费用。
  5. 等待费用和里程费用相加四舍五入后输出总价。

本文地址:https://blog.csdn.net/qq_24483663/article/details/107408958

《PAT 2.出租车计价 python实现.doc》

下载本文的Word格式文档,以方便收藏与打印。