HDU,hduacm
Problem DescriptionHere is a game for two players. The rule of the game is described below:
● In the beginning of the game, there are a lot of piles of beads.
● Players take turns to play. Each turn, player choose a pile i and remove some (at least one) beads from it. Then he could do nothing or split pile i into two piles with a beads and b beads.(a,b > 0 and a + b equals to the number of beads of pile i after removing)
● If after a player's turn, there is no beads left, the player is the winner.
Suppose that the two players are all very clever and they will use optimal game strategies. Your job is to tell whether the player who plays first can win the game.
InputThere are multiple test cases. Please process till EOF.
For each test case, the first line contains a postive integer n(n < 105) means there are n piles of beads. The next line contains n postive integer, the i-th postive integer ai(ai < 231) means there are ai beads in the i-th pile.
OutputFor each test case, if the first player can win the game, ouput "Win" and if he can't, ouput "Lose"
Sample Input
1121 131 2 3
Sample Output
WinLoseLose題意:取完石頭後,可以再把這堆分成兩個堆思路:類似Nim遊戲:異或起來0為先手輸#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>typedef __int64 ll;using namespace std;int main() {int n;while (scanf("%d", &n) != EOF) {ll ans = 0;ll tmp;for (int i = 0; i < n; i++) {scanf("%I64d", &tmp);ans ^= tmp;}if (ans == 0)printf("Lose\n");else printf("Win\n");} }
hdu acm 1052
嘗試:
4
1 2 4 5
2 3 3 4
0
關於速度慢是因為排序,嘗試qsort。
——————————————————————————————
qsort見:
www.cppreference.com/wiki/c/other/qsort
——————————————————————————————
2
2 3
1 3
0
再有什麼問題追加點分吧
簡單題hdu 1004 Runtime Error(ACCESS_VIOLATION)
問題真不少...下面代碼能ac
STACK_OVERFLOW是棧空間溢出,什麼是棧請自行google,你這裡表現為a開得太大了,將大數組開到外面就可以解決。
ACCESS_VIOLATION一般是數組越界了,你這裡表現為b開的太小,當n>100時,i、j下標就會越界
#include<stdio.h>
#include<string.h>
char a[1001][20]; //500改小就成WA,改大就STACK_OVERFLOW
int main()
{
int b[1001],i,maxi,max,j,n;
while(scanf("%d",&n)!=EOF)
{
if(n==0) break; //題幹給出的結束條件,請不要忽略
for(i=0;i<n;i++)
scanf("%s",a[i]);
for(i=0;i<n;i++)
{
b[i]=0;
for(j=i+1;j<n;j++)
if(strcmp(a[j],a[i])==0)
b[i]++;
}
max=b[0];
for(i=0;i<n;i++)
if(b[i]>=max)
{ max=b[i];
maxi=i;
}
printf("%s\n",a[maxi]);
}
return 0;
}