Monkey Choice King (Joseph question) Java implementation

Source: Internet
Author: User

problem
Monkey Choose King, assume that there is a circle of M monkeys, starting from 1, Count to n the monkey quit, the last remaining monkey is the king,
Write the program to find out the last is the king of the monkey's original position.
Problem Solving Analysis:
This question is obviously Joseph's problem, and for such a problem, it is customary to mark each one and then change the mark.
Code:
Package Cocalar;import Java.util.scanner;public class Monkey {public static void Main (string[] args) {TODO auto-generated Method StubDefine the total number of monkeys, and be kicked out of the monkeys are the first fewint n;int number;Enter the number of monkeysScanner cc = new Scanner (system.in);System.out.println ("Please enter the total number of monkeys");n = cc.nextint ();System.out.println ("Please enter the number to the first few monkeys start to exit");Number = Cc.nextint ();Initialize, Mark all the monkeys first as 1Int[] A = new int[n];for (int i = 0; i < a.length; i++){A[i] = 1;}int leftcount = 15;Number of remaining monkeysint countnum = 0;Now count to the first fewint index = 0;Defines the current position starting at 0.If the left number of the current point is not 1Change the kicking monkey marker to 0, not kicked out of the same, still 1while (leftcount! = 1) {if (a[index] = = 1){If the current number of remaining monkeys is greater than 1, then the marker is 1, then add 1 to the counter.countnum++;The number of counters and set the number of monkeys kicked out of the same time, kicked out of the monkey, the mark changed to 0if (Countnum = = number) {Countnum = 0; Refreshes the counter, initialized to 0A[index] = 0; Change the current marker to 0leftcount--;Minus one in the remaining monkeys.}}Index + +; Change the current positionChange the length of an existing monkeyWhen you get to the end, go to the first positionif (index = = a.length) {index = 0;}}From the first to the last one to find which token is 0, if it is 0, the description is kicked out, if it is 1, then the remaining Monkey Kingfor (int i = 0; i < a.length; i++) {if (a[i] = = 1) {System.out.println ("First" + (i+1) + "Only monkeys are chosen as King!");}}}}
There is also a mathematical solution:
http://blog.csdn.net/u012965373/article/details/17076929

Monkey Choice King (Joseph question) 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.