<> Poker problem :

An interesting card drawing problem :54 Playing cards , Two people take turns to get cards , Each person takes at least one at a time 1 Card , At most 4 Card . Whoever takes the last card loses . Write a method for computer to win by taking cards first .

We can consider this problem like this : At the last round , When the machine finishes taking cards, there is only one card left on the field , So the opponent can only take one card , So the machine must win .

Briefly explain the idea : At the last round , Remaining on the court {2,3,4,5} Card ( It means that there are left on the field 2 or 3 or 4 or 5 Card )
The machine will win , Because the machine takes cards first in each round , The number of cards is increasing again 1~4, So you can control the card, and there's only one left . But if the rest 6 Card , The machine is a must . Because after the machine takes the cards, the remaining cards must be {2,3,4,5}, Get the cards across the street , Similarly, the opposite side can also control only one card , So the machine will take the last card .

You can see from the picture above , As long as the machine takes the card first , And the remaining cards in each round are in the left range , Machines are bound to win , Because every time the machine takes a card , Always let the number of cards left on the field make the opposite lose .

Then analyze the table above , Our ingenious discovery , If the cards on the court are right 5 Seeking remainder 1 Words , Then the person who takes the card first will lose .

<> Principle analysis :

The number of cards per person in each round is increasing 1~4. That is, the number of cards taken by two people in a round. As long as one of them is willing, the total number of cards taken in this round can be controlled to be 5.( for instance A take 1 Zhang ,B Just take it 4 The total number of cards you can take in this round is 5.)
Then why not 6,3 Or other numbers ? Because the card holder can't control it , Rule only allowed for 1~4 Zhang .A take {1,2,3,4},B Just take it {4,3,2,1} that will do .

Do you know how many cards you can control in each round 5 What's the use ? If the number of cards remaining on the field is 5 Yes n Doubly 1(n>=1), such as 6,11,16. Then the later card holder can always control the number of cards 5, Then remove it every round 5 individual . At the last round , There is only one card left in the field , You took the cards first again , So you have to lose .

The problem is just the opposite of what we just analyzed , Ask who wins first . In fact, the principle is the same . As long as the rest of the field is not 5 Yes n Doubly 1, Then you take the cards first and you will win .

<> code implementation (python)
import random class DrawCards: def __init__(self): self.Cards=54 def
machineDraw(self): while(1): n=random.randint(1,4) if (self.Cards-n-1)%5==0:
self.Cards-=n print('machine draw ',n,'crads') break def manDraw(self): while(1)
: n=input('pleasr input a numbee in 1~4\t') if int(n) in (1,2,3,4): self.Cards-=
int(n) break print('error,please enter again') def Run(self): count=0 while(self
.Cards!=0): self.machineDraw() self.manDraw() count+=1 print('End of round ',
count,',there ',self.Cards,'left on the field\n') if self.Cards in (2,3,4,5):
print('machine draw',self.Cards-1,'cards,you loss!') break if __name__ ==
"__main__": start=DrawCards() start.Run()

Technology
©2019-2020 Toolsou All rights reserved,
What is a process , Concept of process ? Tang Seng team to lay off staff , Who will you lay off ?Oracle Database access performance optimization JVM Old age garbage collection Full GCweb Two front-end practical games ( Attached source code ) about linux command “iptables -F”, Don't carry it out easily navicat function sql File error 2021-06-03 A man is not born to be defeated Pandas And openpyxl Super combination of Library , bye ,Excel! It is never recommended to spend a lot of time studying for employment python