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