HDU1003最大连续子序列和

很惭愧,一开始定义最大值为0,WA了好几次,最后把最大值初始为-1001,才AC。

还是不能大意。

 1 #include <iostream>
 2 using namespace std;
 3 int main(){
 4     //freopen("in.txt","r",stdin);
 5     int T;
 6     cin>>T;
 7     int num =1;
 8     while(T--){
 9         int n;
10         cin>>n;
11         int i,a;
12         long long  maxsum = -1001;
13         int start=0,end=0;
14         long long tempsum = 0;
15         int tempstart = 1;
16         for(i=0;i<n;i++){
17             cin>>a;
18             tempsum = tempsum + a;
19             if(tempsum>maxsum){
20                 maxsum = tempsum;
21                 start = tempstart;
22                 end = i+1;
23             }
24             if(tempsum<0){
25                 tempsum = 0;
26                 tempstart = i+2;
27             }
28         } 
29         cout<<"Case "<<num++<<":"<<endl;
30         cout<<maxsum<<" "<<start<<" "<<end<<endl;
31         if(T) cout<<endl;
32     } 
33     return 0;
34 }

 

posted @ 2019-09-17 19:13  清谗  阅读(123)  评论(0编辑  收藏  举报