Description
Input
Output
Sample Input
Sample Output
Case 1: 14 1 4 Case 2: 7 1 6
#include<cstdio>
using namespace std;
#define INF 0x7fffffff
int const maxn=100000+10;
int a[maxn];
int main()
{
int kcase=1;
int T,n,max,star,end,pos,sum;//max爲最大字段和,star爲枚舉起始位置,end爲枚舉終止位置,sum爲字段和,pos用來更新枚舉起始位置
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
max=-INF;
pos=sum=0;
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
if(sum>max)
{
max=sum;
star=pos;
end=i;
}
if(sum<0)
{
sum=0;
pos=i+1;
}
}
printf("Case %d:\n",kcase++);
printf("%d %d %d\n",max,star+1,end+1);//數組下標是從0開始的,而數的位置是從1開始的
if(T)
printf("\n");
}
return 0;
}