连续子数组最大和

#include<stdio.h> 
#include<string.h>

int max(const int *a, int n){
	int i;
	int currentMax= 0;
	int lastMax= 0;
	for(i=0; i<n; ++i){
		if(a[i]>= 0){
			if(currentMax+ a[i]< a[i]){
				currentMax= a[i];
				printf("%d\n", currentMax);
				lastMax= a[i];
				printf("%d\n", lastMax);
			}else{
				currentMax += a[i];
				printf("%d\n", currentMax);
				if(currentMax> lastMax){
					lastMax= currentMax;
				} 
			}
		}else{
			currentMax += a[i];
			printf("%d\n", currentMax);
		}
	}
	return lastMax;
} 

int main(void){
	int a[]={1, -2, 3, 10, 11, -9 ,12, 21, -9, 12, -4, 7, 2, -5};
	printf("%d\n", max(a, 14));
	
}

posted @ 2014-09-02 19:46  徐小鱼  阅读(148)  评论(0编辑  收藏  举报