View Code of Problem 19

#include<stdlib.h>
#include<string.h>
#include<math.h>
#include "stdafx.h"
#include<stdio.h>

int main(int argc, char** argv)
{
	/*
	给定n个整数(可能为负数)组成的序列a[1],a[2],a[3],…,a[n],
	求该序列如a[i]+a[i+1]+…+a[j]的子段和的最大值。
	6	
-2 11 -4 13 -5 -2
	*/
	int n;
	while( scanf("%d" , &n)!=EOF ){
		if( n <= 0)
			break;
		int a[1000];
		for(int k = 0 ; k < n ; k++)
			scanf("%d" , &a[k]);
		int max = a[0];
		for(int i = 0 ; i < n ; i++){
			int sum = 0;
			for(int j = i + 1 ; j < n ; j++){
				sum += a[j];
				if(max < sum)
					max = sum;
			}
		}
		if(max < a[n - 1] )
			max=  a[ n - 1];
		printf("%d\n" , max);

	}
	return 0;
}

/*
Main.c:5:20: fatal error: stdafx.h: No such file or directory
 #include "stdafx.h"
                    ^
compilation terminated.
*/

Double click to view unformatted code.


Back to problem 19