python 练习题- 最大收益

题目:

 1 第一列为商品成本价格
 2 第二列为商品卖出价格
 3 第三列为本金
 4 要求:
 5 1.每种商品只能买入卖出一次
 6 2.求最大收益
 7 
 8 例子:
 9 输入:
10 3,1,5,4,3
11 4,7,6,6,4
12 16
13 
14 输出:
15 27
16 (先买入前四种,然后卖出,再买入第五种)

 

代码:

 1 # @Author  :whyCai
 2 # @Time    :2021/2/23 22:00
 3 
 4 import sys
 5 if __name__ == "__main__":
 6     # 取值
 7     cost = sys.stdin.readline().strip()
 8     sell = sys.stdin.readline().strip()
 9     price = int(sys.stdin.readline().strip())
10     cost = list(map(int, cost.split(',')))
11     sell = list(map(int, sell.split(',')))
12 
13     #取成本和卖出价格差
14     profit = list(map(lambda x: x[1]-x[0], zip(cost, sell)))
15     sur = price
16     #一个一个取值,如果成本价大余额,则跳出
17     for i in range(len(cost)):
18         if sur > cost[i]:
19             surNew = sur - cost[i] + profit[i]
20             sur = surNew
21         else:
22             break
23     endPrice = price + sur
24     print(endPrice)

 

posted @ 2021-02-23 22:30  菜小鱼~  阅读(446)  评论(0编辑  收藏  举报