思路:
计算差值: 后一天的价格 - 前一天的价格
def maxProfit(list1):
dict1={}
if len(list1)<=1:
print('无收益')
else:
for i in range(len(list1)):
if i>=len(list1)-1:
break
else:
if list1[i+1]-list1[i]>0:
dict1[list1[i+1]-list1[i]]=[list1[i+1],list1[i]]
for key,values in dict1.items():
print('%s买入,%s卖出,收益:%s'%(values[0],values[1],key))
print('最大收益:',sum(dict1.keys()))
if __name__=="__main__":
import random
listOne =[77, 84, 59, 56, 69, 38, 53, 77, 35, 89]
print(listOne)
maxProfit(listOne)
调试结果: