来学习学习 看谢 hengbang 捞 本帖最后由 yu123py 于 2019-4-29 15:41 编辑
暴力穷举法,没想到最好的办法。
def maxSequence(l):
s = 0
for i in range(0, len(l)-1):
for j in range(i+1, len(l)):
if sum(l) > s:
s = sum(l)
return s jerryxjr1220 发表于 2017-4-27 21:20
这题可以用动态规划算法,这样只要1次循环就能得结果,而不需要循环中间套循环了。
思路就是先求局部最大 ...
一脸懵~,这和切片的和有关系么 def fun35(L):
Maximum = max(L)
for i in range(len(L)):
for j in range(i+1,len(L)):
if (sum(L)>Maximum):
a = i
b = j
Maximum =sum(L)
print(Maximum,L)
显示最大值,并显示切片数列 ls =[-2, 1, -3, 4, -1, 2, 1, -5, 4]
print(ls)
max = 0
for i in range(1,len(ls)):
for k in range(2,len(ls)+1):
sum = 0
for r in ls:
sum += r
if sum> max:
max = sum
print(max)
的 1 学习 a = [-6, 21, -4, 19, -27, 22, -19, -27, 2, 20, -16, 26, 5, 13, -14, 22, -17, 23, -7, -8, 3, 26, -11, -28, 15, -21, -6,-22, 24, -2, -29, 28, 22, -6, 17, 4, -29, 3, 8, 2, -18, -1, -9, -23, 9, -18, 17, 15, 23, 29]
list1 , list2=[],[]
len1 = len(a)
for i in range(len1):
for j in range(1,len1+1):
if i < j:
list1.append(a)
for k in list1:
list2.append(sum(k))
if max(list2) > 0:
result = list1
else:
result = 0
print("最大的切片是:")
print(result)
print("总数为:")
print(max(list2)) 666 1
页:
1
[2]