zoukankan      html  css  js  c++  java
  • python 练习题- 图书租金计算

    题目:

     1 每个用户初始金额300
     2 
     3 1、扣除规则
     4 1)图书价格大于等于 100元,累计借出去天数小于等于15天,每本每天租金5元,累计超过15天的,超过的天数每天每本租金3元
     5 2)图书价格大于等于 50元、小于100元,累计借出去天数小于等于15天,每本每天租金3元,累计超过15天的,超过的天数每天每本租金2元
     6 3)图书价格小于 50元,每本每天租金1元
     7 
     8 2、余额小于所借图书价格,不能借;可以继续借阅其他的书
     9 
    10 3、租金最大不超过所借图书的价格
    11 
    12 4、超期还书,每超过一天,另外扣1元
    13 
    14 输出余额
    15 
    16 例子:
    17 [[130,5,11],[60,16,16],[10,11,11]]
    18 
    19 输出:
    20 182
    21 
    22 解释:
    23 130,5,11   实际11天,超期6天,则租金为 11*5+6 = 61
    24 60,16,16   实际11天,有一天超过15天,则租金为 15*3 + 2 = 47
    25 10,11,11   实际11天,11*1 = 11,大于图书价格10元,则租金为10
    26 总共为 61+47+10 = 118,余额 182

    代码:

     1 # @Author  :whyCai
     2 # @Time    :2021/2/22 9:44
     3 
     4 
     5 from typing import List
     6 """
     7 计算剩余金额
     8 """
     9 class Solution:
    10     def chargeBalance(self, histList: List[List[int]]) -> int:
    11         initBalance = 300
    12 
    13         histListLen = len(histList)
    14         for i in range(histListLen):
    15             #图书价格小于余额
    16             if histList[i][0] < initBalance:
    17                 #判断图书价格再哪个区间
    18                 if histList[i][0] >=100:
    19                     #实际借书日期是否再15天内
    20                     chargeBalance = histList[i][2] * 5 if histList[i][2] <= 15 else 15 * 5 + (histList[i][2] -15) * 3
    21                 elif histList[i][0] >=50 and histList[i][0] <100:
    22                     chargeBalance = histList[i][2] * 3 if histList[i][2] <= 15 else 15 * 3 + (histList[i][2] -15) * 2
    23                 else:
    24                     chargeBalance = histList[i][2] * 1
    25                 # 判断是否超期还书
    26                 if histList[i][2] > histList[i][1]:
    27                     chargeBalance = chargeBalance + (histList[i][2] - histList[i][1]) * 1
    28                 #判断最大租金不超过所借图书价格
    29                 if chargeBalance > histList[i][0]:
    30                     chargeBalance = histList[i][0]
    31                 initBalance = initBalance - chargeBalance
    32 
    33         return initBalance
    34 
    35 print(Solution().chargeBalance([[130,5,11],[60,16,16],[10,11,11]]))
  • 相关阅读:
    JavaScript操作符instanceof揭秘
    Linux打开txt文件乱码的解决方法
    Working copy locked run svn cleanup not work
    poj 2299 UltraQuickSort 归并排序求解逆序对
    poj 2312 Battle City 优先队列+bfs 或 记忆化广搜
    poj2352 stars 树状数组
    poj 2286 The Rotation Game 迭代加深
    hdu 1800 Flying to the Mars
    poj 3038 Children of the Candy Corn bfs dfs
    hdu 1983 Kaitou Kid The Phantom Thief (2) DFS + BFS
  • 原文地址:https://www.cnblogs.com/whycai/p/14429880.html
Copyright © 2011-2022 走看看