USTC机试—计算连续子序列之和的最大值
写的很好http://blog.csdn.net/zzl913657644/article/details/52431011此处有四中方法,我这记录了最简单的方法
//求最大子序列之和,最优起点法,三重循环法外两重是起点和终点,两重循环法
//最优起点法如下,负数肯定不是起点,前n项为赋值肯定也不是起点
#include<stdio.h>
#define N 100
int main(){
int a[N];
int sum=0;
int n;
while(scanf("%d",&n)){
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
int max=0;
for(i=0;i<n;i++){
sum+=a[i];
if(sum>max)max=sum;//行进过程中记录最大值
else{
sum=0;//重新计算起点
}
}
printf("%d\n",max);
}
return 0;
}