首页 > 编程语言 > 详细

python 练习题- 最大收益

时间:2021-02-23 23:14:21      阅读:23      评论:0      收藏:0      [点我收藏+]

题目:

 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)

 

python 练习题- 最大收益

原文:https://www.cnblogs.com/whycai/p/14438620.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!