Joseph Ring in gambling algorithms (JAVA)

Source: Internet
Author: User

[Java]/*** story: it is said that the famous Jewish historian Joseph PHUs had the following story, 39 Jews and Joseph and his friends hid in a cave. * 39 Jews decided to die rather than be attacked by the enemy, so they decided to commit suicide. 41 people arranged in a circle, the number of reports starts from 1st. Each report reaches 3rd. * This person must commit suicide, and then report again from the next report until all people commit suicide. However, Joseph and his friends did not want to follow the game. Joseph asked * his friends to pretend to follow the game first. He arranged his friends and himself in 16th and 31st positions, so he escaped the game of death. * @ Param num Number of people * @ param per number of people to extract a * @ return Joseph Ring array */public class Joseph PHUs {public static int [] arrayofjoseph PHUs (int num, int per) {int [] nums = new int [num]; int count = 0; int pos = 0; for (int I = 1; I <= num; I ++) {while (true) {pos = pos % num; if (nums [pos] = 0) {count ++;} if (count = per) {count = 0; break;} pos ++;} nums [pos] = I;} return nums;} // implement public static void main (String [] args) {int manNum = 41; int per = 3; int aliveNum = 3; int [] man = Joseph. arrayofjoseph PHUs (manNum, per); System. out. println ("yoqinfu ring:" + Arrays. toString (man); System. out. println ("L indicates where three surviving persons are to be put:"); for (int I = 0; I <manNum; I ++) {if (man [I]> manNum-aliveNum) System. out. print ("L"); else System. out. print ("D"); if (I + 1) % 5 = 0) System. out. print ("");}}}

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.