hdu 1231_最大字段及其起始、終止位置

原題鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1231

#include<stdio.h>
#include<stdlib.h> 
int n,i,a[10005],b,thisSum,thisi,thisj,sum; 
int tempi,tempj;
bool flag;
int main()
{
	while(~scanf("%d",&n))
	{
		if(n == 0) break; 
		b = 0,thisSum = 0,sum = -1,thisi = 0,thisj = 0;
		tempi = 0,tempj = 0;
		flag = true;
		for(i = 0; i < n; i++) 
		{
			scanf("%d",&a[i]);
			if(a[i] >= 0) flag = false;
		}
		if(flag)
		{
			printf("%d %d %d\n",0,a[0],a[n - 1]);
			continue;
		}
		for(i = 0; i < n; i++)
		{
			if(b > 0)
				b += a[i];
			else b = a[i],tempi = i;
			if(b > sum)
			{
				sum = b;
				thisi = tempi;
				thisj = i;
			}
		}
		printf("%d %d %d\n",sum,a[thisi],a[thisj]);
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章