计算最小数组环

队友:黄山成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

三、运行结果

posted @ 2016-03-27 17:58  Megau_Bing  阅读(205)  评论(1编辑  收藏  举报