m0_52179328
2021-01-18 18:31新手,在VJ上提交时显示wrong answer,是那里错啦?十分感谢
/*Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.
Input
The 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).
Output
For 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 main()
{
int T;
cin>>T;
while(T--)
{
int N;
cin>>N;
int* num=new int[N];
int* sum=new int[N];
int i=0;
while(i<N)
{
cin>>num[i];
i++;
}
sum[0]=num[0];
for(i=1;i<N;i++)
{
sum[i]=num[i]+sum[i-1];
}
int max=sum[0],begin,end;
for(i=0;i<N;i++)
{
if(num[i]>=0)
{
begin=i+1;
break;
}
}
for(i=0;i<N;i++)
{
if(max<sum[i])
{
max=sum[i];
end=i+1;
}
}
if(end==0)
{
end=1;
}
static int ca=1;
if(N>1)
{
cout<<"Case "<<ca<<":"<<endl;
cout<<max<<" "<<begin<<" "<<end;
}
if(N==1)
{
cout<<"Case "<<ca<<":"<<endl;
cout<<num[0]<<" 1"<<" 1";
}
ca++;
if(T)
{
cout<<endl;
cout<<endl;
}
if(T==0)
{
cout<<endl;
}
}
return 0;
}
- 点赞
- 回答
- 收藏
- 复制链接分享
0条回答
为你推荐
- 在Golang中从base64渲染图像
- http
- 1个回答
- 空白电子邮件,使用PHP发送电子邮件时没有错误[关闭]
- html
- php
- 1个回答
- PHP preg_match抛出未知修饰符[重复]
- php
- 2个回答
- *和&的转化出现了问题?请看代码的注释行,改动前的代码是可以运行的。
- c++
- 1个回答
- jQuery在PC上运行但在WAMP或Shared Server上运行
- json
- html
- php
- jquery
- 1个回答
换一换