m0_52179328
m0_52179328
2021-01-18 18:31

新手,在VJ上提交时显示wrong answer,是那里错啦?十分感谢

  • c++
  • c语言
/*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条回答