Java implementation: victorious general problem

Java implementation: victorious general problem

1. Question

Victory General Problem: There are 21 matches. Two people take turns to take them. Each person can take 1 to 4 at a time. They cannot take more or not take none. Whoever takes the last match loses. Please write a program to play human-computer chess, requiring the human to take first, and the computer to take second; the computer side is the "always victorious general".

2. Solution

Leave the last match to the human, and the computer wins. There are 21 roots in total, excluding the last one, it is 21 - 1 = 20. Now, the problem becomes how to solve the first 20 matches when the computer retrieves them for the last time.

The advantage is that the person takes it first, and the computer takes it later. In this way, no matter how many sticks the person takes, the computer can decide how many to take based on the number taken by the person, so that the number obtained in each round is equal, and finally it can be achieved 每一轮的数量 X 轮数 = 20.

The quantity in each round needs to be divisible by 20. The options are 1, 4, 5, and 20;

When the human takes the maximum value 4, the computer takes the minimum 1, which can be calculated

Guess you like

Origin blog.csdn.net/qq_36462452/article/details/131028484