About Joseph Ring algorithm, array resolution

Source: Internet
Author: User

It is said that the famous Jewish historian Josephus had the following story: After the Romans occupied Chotapat, 39 Jews and Josephus and his friends hid in a hole, 39 Jews decided to prefer to die not to be caught by the enemy, so decided a suicide, 41 people into a circle, By the 1th person starts to count, each count to the 3rd person The person must commit suicide, and then again by the next re-count, until everybody commits suicide to die. However, Josephus and his friends did not want to follow. First begins with a person, crosses the k-2 individual (because the first person has been crossed), and kills the nth person. Then, cross the K-1 and kill the K -man. The process continues along the circle until only one person remains in the end, and the person can continue to live. The question is, given and, where do you stand at the outset to avoid being executed? Josephus to his friends to pretend to obey, he put friends and himself in the 16th and 31st position, so escaped the game of death.

The following Java code implementation algorithm

 PackageCom.gh.p8;/*** Created by Lenovo on 2014/12/4.*/ Public classP8_2 {Static Final intNum=41; Static Final intKill_num=3; Static voidJosephusintalive) {        intPos=-1; int[] man=New int[NUM]; intCount=1; intI=0;  while(Count<= (num-alive)) {             Do{pos= (pos+1)%NUM; if(man[pos]==0) {i++; }                if(i==kill_num) {i=0;  Break; }            } while(true); Man[pos]=count; Count++; }         for(intj = 0; J < Man.length; J + +) {System.out.println (man[j]); }    }     Public Static voidMain (string[] args) {Josephus (3); }}

About Joseph Ring algorithm, array resolution

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.