View Code of Problem 19

#include<stdio.h>
int main() {
	int n;
	while(scanf("%d",&n)!=EOF && n!=0) {
		int a[n];
		for(int i=0; i<n; i++){
			scanf("%d", &a[i]);
		}
		
		int max = a[0];
		for(int i=0; i<n; i++){ //两层for循环暴力运算 
			int sum = 0; //多组测试数据,所以要刷新sum的值
			for(int j=i; j<n; j++){
				sum += a[j];
				if(sum > max){
					max = sum;	//不断更新max的值 
				}
			}
		}
		
		printf("%d\n", max);
	}
	
	return 0; 
}

Double click to view unformatted code.


Back to problem 19