商场购物优惠计算器 - Python 代码实现

本页面提供商场周末购物优惠计算器的 Python 代码实现,帮助用户计算最终的应付款。

优惠规则: 凡购物超过100元时,超过100元那部分便可打9折。

输入描述:

  • 第一行为整数N,表示所买物品的个数。 (N <= 100)
  • 第二行为N个空格隔开的整数,表示N件物品要付的元数。

输出描述:

  • 最终的应付款。(保留两位小数)

用例输入 1 :

5
60 80 50 30 20

用例输出 1 :

226.00

解法一:

  1. 读取输入的N和N个物品的价格;
  2. 初始化应付款total为0;
  3. 遍历物品的价格,对于每个物品的价格price,如果price大于100,将超过100的部分按9折计算,加入total;
  4. 输出total。
N = int(input())
prices = list(map(int, input().split()))

total = 0
for price in prices:
    if price > 100:
        total += (price - 100) * 0.9
    total += price

print("{:.2f}".format(total))

解法二:

  1. 读取输入的N和N个物品的价格;
  2. 使用列表推导式将超过100的物品价格按9折计算,将所有物品价格相加得到总价total;
  3. 输出total。
N = int(input())
prices = list(map(int, input().split()))

total = sum([(price - 100) * 0.9 if price > 100 else price for price in prices])

print("{:.2f}".format(total))
商场购物优惠计算器 - Python 代码实现

原文地址: https://www.cveoy.top/t/topic/qjC5 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录