1. 程式人生 > >(新生賽問題 )B: Alice and Bob

(新生賽問題 )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; }