标签:lines cas input 连续 ace cin 表示 test 结束
InputThe first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).
OutputFor each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.
Sample Input
2 5 6 -1 5 4 -7 7 0 6 -1 1 -6 7 -5
Sample Output
Case 1:
14 1 4
Case 2:
7 1 6
#include <iostream> using namespace std; int v[100000]; int main() { int m; cin>>m; for(int q=1;q<=m;q++) { int n,sum=0,maxsum=-100000,st=0,en=0; cin>>n; for(int i=0;i<n;i++) { cin>>v[i]; } int k=1; for(int i=0;i<n;i++) { sum+=v[i]; if(maxsum<sum) { maxsum=sum; en=i+1; st=k; } if(sum<0) { sum=0;//当sum<0时 更新sum为0; k=i+2; } } cout<<"Case "<<q<<":"<<endl; cout<<maxsum<<" "<<st<<" "<<en<<endl; if(q!=m) cout<<endl; } return 0; }
标签:lines cas input 连续 ace cin 表示 test 结束
原文地址:https://www.cnblogs.com/xuxua/p/9275325.html