1. Description of the problem:
There are n balls in this box, a, b two people take the ball from the box in turn, and everyone can see the number of the other's balls.
The provisions are as follows: Take the ball can only take 1,3,7,8 four kinds of circumstances. If no ball is taken, it loses. Rule a first to take the ball, given the number of initial ball, both sides do not make mistakes, in order to best determine whether a can win. For example: Only 1 ball, a first take 1 ball, then B no ball can take, then B lost, a can win. 2 Ball, a lost.
2. Algorithm idea:
The idea of this problem is to traverse each method, similar to the horse walking day, traversing each kind of walk. Then judge the other side is to lose or win, if the other side can win, then I will continue to change the way, if the other side lost, then I won.
3. code example:
Package Test;public class Java game problem {static int weizhi[] = {1,3,7,8};p ublic static void Main (string[] args) {System.out.pri Ntln (f (2)); System.out.println (f (10)); System.out.println (f (1)); System.out.println (f (4)); System.out.println (f (8));} Private static Boolean f (int n) {int next_n = 0;for (int i=weizhi.length-1;i>=0;i--) {if (N>=weizhi[i]) {next_n = N-we Izhi[i];if (f (next_n)) {}else {return true;}}} return false;}}
The game problem of taking the ball in Java implementation