题目描述 Description
给定一个长度为n的一个序列A1,A2,…,An,求序列中连续子序列的最大和。
例如:当输入为-5,3,5,7,-15,6,9,27,-36,10时,连续子序列6,9,27的和为42是最大值;而当序列变成-5,3,5,8,-15,6,9,27,-36,10时,连续子序列3,5,8,-15,6,9,27的和为43是最大值。
输入描述 Input Description
第一行为n (n≤1000),第二行为n个数,表示序列Ai(-10000≤Ai≤10000)。
输出描述 Output Description
一个数,表示连续子序列的最大和。
样例输入 Sample Input
10
-5 3 5 8 -15 6 9 27 -36 10
样例输出 Sample Output
43
1 #include<cstdio> 2 3 int n; 4 5 int a[1000+10]; 6 int b[1000+10]; 7 int ans=-1000000; 8 9 int main() 10 { 11 scanf("%d",&n); 12 for(int i=1;i<=n;i++) 13 scanf("%d",&a[i]); 14 for(int i=1;i<=n;i++) 15 for(int j=i+1;j<=n;j++) 16 { 17 int p=0; 18 for(int k=i;k<=j;k++) 19 p+=a[k]; 20 if(p>ans) ans=p; 21 } 22 printf("%d",ans); 23 return 0; 24 }
-10000≤Ai≤10000
n≤1000