[ZCMU OJ]1276/1226: 求和游戏(序列中指定元素个数的最大和)

Description

小明有n个球排成一行,每个球上有个数字。现在小明选择两个球,使这两个球及其这两个球之间的数字和最大。如果这个最大的和不为正,则输出“Game Over”。

Input

第一行输入T,有T组数据。

每组数据:输入n(1

Output

对于每组数据,输出最大和或者”Game Over“,占一行。

Sample Input

6

5

3 -5 7 -2 8

3

-1 2 3

4

-1 1 -111 1111

3

-1 -2 -3

3

1 -1 0

6

100 -1 -1 -1 -1 -1 -1

Sample Output

13

5

1001

Game Over

Game Over

99

HINT

Source

SJTU

_____________________________________________________________________________

这道题和最大子序列和有异曲同工之处。

利用temp保存第一个与第二个数之和。若temp大于第二个数,说明第一(二)个数是大于0的数;反之,说明第一(二)个数是小于0的数,则跳过第一个数,保存第二个数与第三个数之和。以此类推;并随时记录temp的最大值。

#include
const int maxn=1e6+10;
double a[maxn]={0};
using namespace std;
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		int n;
		cin>>n;
		for(int i=0;i>a[i];
		
		int temp=a[0]+a[1];
		int max=temp;//细节 
		
		for(int i=1;ia[i])
			temp+=a[i+1];
			else
			temp=a[i]+a[i+1];
			
			if(temp>max)
			max=temp;
		}

		if(max>0) 
		cout<

你可能感兴趣的:(zcmuoj,c++,学习)