01-复杂度2. Maximum Subsequence Sum (25)

http://www.patest.cn/contests/mooc-ds2015spring/01-复杂度2

题目是陈越老师的课求最大子序列病输出子序列开始和结尾的数字。这道题部分正确有待改正

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
int main(){
	int num;
	scanf("%d",&num);
	int *arr=(int *)malloc(num*sizeof(int));
	int i=0;
	for(i=0;i<num;i++)
	{
		scanf("%d",arr+i);
	}
	
	int j=0,thissum=0,maxsum=0,start=0,end=0,start1=0;
	 	for(j;j<num;j++){
	 		thissum+=arr[j];
			if(thissum>maxsum){
	 			maxsum=thissum;
	 			start1=start;
	 			end=arr[j];
	 		}
	 		
	 		else if(thissum<0){
	 			thissum=0;
	 			if (j+1<=num)
	 			{
	 				start1=start;
	 				start=arr[j+1];
	 			}
	 			
	 		}
	 		
	 	}
	 printf("%d %d %d",maxsum,start1,end);

	return 0;
}


你可能感兴趣的:(C++,malloc)