解题报告:hdu1003 Max Sum - 最大连续区间和 - 计算开头和结尾

时间:2021-07-09 17:00:48

2017-09-06 21:32:22

writer:pprp

可以作为一个模板

/*
@theme: hdu1003 Max Sum
@writer:pprp
@end:21:26
@declare:连续区间最大和
@data:2017/9/6
*/ #include <bits/stdc++.h> using namespace std; int main()
{
//freopen("in.txt","r",stdin);
int cas;
cin >> cas;
for(int l = ; l <= cas; l++)
{
int n;
cin >> n;
int tmp = , ans = -, data2;
int op = , ed = , k = ; for(int i = ; i <= n ; i++)
{
cin >> data2;
tmp += data2; if(ans < tmp)//两个顺序不能调换,因为op这里要用上一个k
{
ans = tmp;
ed = i;
op = k;
} if(tmp < )
{
tmp = ;
k = i + ;
}
}
if(l != )
cout << endl; cout << "Case " << l << ":" << endl; cout << ans << " " << op << " " << ed << endl;
} return ;
}