def maxSum(list):
maxsum = list[0]
for i in range(len(list)):
maxtmp = 0
for j in range(i,len(list)):
maxtmp += list[j]
if maxtmp > maxsum:
maxsum = maxtmp
return maxsum
if __name__ == '__main__':
list = [1,3,-3,4,-6]
maxsum = maxSum(list)
print "maxsum is",maxsum def maxSum(list_of_nums):
maxsum = 0
maxtmp = 0
for i in range(len(list_of_nums)):
if maxtmp <= 0:
maxtmp = list_of_nums[i]
else:
maxtmp += list_of_nums[i]
if(maxtmp > maxsum):
maxsum = maxtmp
return maxsum
if __name__ == '__main__':
list_of_num = [1,3,-3,4,-6]
maxsum = maxSum(list_of_num)
print "maxsum is: ",maxsum maxsum is 5
以上内容就是用Python语言描述最大连续子序列和教程,希望对能帮助到大家。
相关推荐:
最大连续子序列和问题
完全掌握 Python
python版简单工厂模式的介绍
以上就是用Python语言描述最大连续子序列和的详细内容,更多请关注Gxl网其它相关文章!
查看更多关于用Python语言描述最大连续子序列和的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did84342