(新生赛问题 )B: Alice and Bob

题目描述
Alice and Bob decide to play a game. At the beginning of the game they put n piles of sweets on the table.The number of each pile of sweets may be different. Alice and Bob take away sweets in turn,and always Alice takes sweets first in every games.Each time in their turn they can get only one whole pile of sweets.When there is no sweet left, the game is over. Finally, The man who have the largest number of sweets in hand will win.
Assume that Alice and Bob were very clever.

输入
The first line is an integer n, which means that there are n pile sweets.
1 <= n <= 100000
Next n integers, the i-th integer means the number of sweets in the i-th pile.
The number of sweets in each pile is less than 10000.

输出
If Alice wins, output “A”, and if Bob wins, output “B”.
Otherwise output “again”

样例输入
样例输入1:
3
1 6 7

样例输入2:
4
3 3 3 3

样例输出
样例输出1:

A

样例输出2:
again

刚开始的想法是先冒泡排序,然后奇数时A拿,偶数时B拿,结果程序超时15%,自己写了个快排。。。。结果超时31%!!what?。。。什么都不懂的我束手无策。。
其实仔细想想Bob根本没赢的机会,也不用排序,就找出平局,其他的都输出A赢就行了。。扎心

#include <stdio.h>
int main()
{
	int n;
	int A=0,B=0;
	int temp;
	int a[100001];
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	scanf("%d",&a[i]);
	for(int i=1;i<=n;i++)
	{
		if(i%2!=0)
		A=A+a[i];
		else if(i%2==0)
		B=B+a[i];
	}
	if(A==B)
	printf("again");
	else
	printf("A"); 
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43332389/article/details/84571540
今日推荐