脑图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/fcf8070a65c816d1941ac33e7f7d73be.png#pic_center)
代码
#include<iostream>
using namespace std;
int MaxSubseqSum1(int n,int a[]) {
int max=0,tmpSum=0;
for(int i=0;i<n;i++) {
for(int k=0;k<i;k++) {
for(int j=k;j<=i;j++) {
tmpSum+=a[j];
}
if(max<tmpSum) max=tmpSum;
}
}
return max;
}
int MaxSubseqSum2(int n,int a[]) {
int max=0,tmpSum=0;
for(int i=0;i<n;i++) {
for(int j=i;j<n;j++) {
tmpSum+=a[j];
}
if(max<tmpSum) max=tmpSum;
}
return max;
}
int Max3(int A,int B,int C) {
return (A>B) ? ((A>C) ? A:C):((B>C) ? B:C);
}
int DivideAndConquer(int a[],int left,int right) {
if(left==right) {
if(0<a[left]) return a[left];
return 0;
}
int center=(left+right)/2;
int MaxLeftSum=DivideAndConquer(a,left,center);
int MaxRightSum=DivideAndConquer(a,center+1,right);
int MaxLeftBorderSum=0, LeftBorderSum=0;
for(int i=center;i>=left;i--) {
LeftBorderSum+=a[i];
if(MaxLeftBorderSum<LeftBorderSum) MaxLeftBorderSum=LeftBorderSum;
}
int MaxRightBorderSum=0, RightBorderSum=0;
for(int i=center+1;i<=right;i++) {
RightBorderSum+=a[i];
if(MaxRightBorderSum<RightBorderSum) MaxRightBorderSum=RightBorderSum;
}
return Max3(MaxLeftSum,MaxRightSum,MaxLeftBorderSum+MaxRightBorderSum);
}
int MaxSubseqSum3(int n,int a[]) {
return DivideAndConquer(a,0,n-1);
}
int MaxSubseqSum4(int n, int a[]) {
int max = 0, tmpSum = 0;
for (int i = 0; i<n; i++) {
tmpSum += a[i];
if (tmpSum<0) {
tmpSum = 0;
}
else if (max<tmpSum) {
max = tmpSum;
}
}
return max;
}
int main() {
int n;
int a[100000];
cin >> n;
for (int i = 0; i<n; i++)
cin >> a[i];
cout<<MaxSubseqSum1(n,a);
cout<<MaxSubseqSum2(n,a);
cout<<MaxSubseqSum3(n,a);
cout << MaxSubseqSum4(n, a);
return 0;
}
def MaxSubseqSum4(n, lst):
max = tmpSum = 0
for i in range(n):
tmpSum += lst[i]
if(tmpSum < 0):
tmpSum = 0
elif(tmpSum > max):
max = tmpSum
return max
def main():
num = int(input("请输入总个数:"))
a = [int(x) for x in input().split(',')]
MaxSubseqSum4(num, a)
if __name__ == '__main__':
main()
print('over')