game-nim game

nim game

There are n piles of several items, two people take turns to take as many items from a certain pile, and it is stipulated that at least one item is taken at a time, and there is no limit to the number of items.

analyze:

For n piles of stones, one case is that each pile is 1, then whoever wins or loses can know by looking at the number of piles;

For not all 1s, if these piles are singular situations, or non-singular situations, but non-singular situations can be converted to odd situations.

For not all 1s: if the initial given is a strange situation, the first to take the loser; if the initial given is a non-singular situation, the first to take the win.

intmain()

{

int i,n,a[101],ans,f;

 

while(scanf("%d",&n)!=EOF)

{

f=0;

years=0;

for(i=0;i<n;i++)

{

scanf("%d",&a[i]);

ans^=a[i];

if(a[i]>1)

f=1;

}

if(f==0)//all 1

{

if(n%2==0)

printf("First mover wins");

else

printf("The next move will win");

}

else

{

if(ans==0)//Odd situation  

printf("First mover will lose");

else

printf("First mover wins");

}

}

return 0;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325162275&siteId=291194637