The game problem of taking the ball in Java implementation

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.