计算最小数组环

队友:黄山成http://home.cnblogs.com/u/surver/

一、分析

先将一个长度为n的数组扩大两倍,即在这个数组后加一个一样的数组,然后依次取长度为n,数组开头元素为原数组的单元构成n个数组。对n个数组依次进行求最大子数组和,然后在这一堆和里找出最大和。

二、代码

str1="4 -2 3 -1 -1 10"
print"请输入一个数组"
str1=raw_input()
counter_1=0
str2=str1+" "+str1
str2=str2.split()
#print str2
counter_1=0

for i in str2:
    counter_1=counter_1+1
counter_1=counter_1/2
#print counter_1
answer_array=[0]*counter_1


for j in range(0,counter_1):
    str3=str2[j:j+counter_1]
    #print"str3",str3

    sum_record_unexcept=0
    str3[0]=int(str3[0])
    sum_constant_add=0

    for i in str3:
        i=int(i)
        sum_record_unexcept=max(sum_record_unexcept,sum_constant_add)
        sum_constant_add=max(sum_constant_add+i,i)
    answer_array[j]=max(sum_constant_add,sum_record_unexcept)
    #print "answer_array[i]",answer_array[j]

#print"answer",answer_array
maxment=answer_array[0]
for i in answer_array:
    if(maxment<i):
        maxment=i

print"最大值为",maxment

三、运行结果

转载于:https://www.cnblogs.com/Megau/p/5326343.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值