Joseph Problem (Java implementation)

Source: Internet
Author: User

 Method One, the custom list implementation

Package com.code.yuesefu;
Public classYuesefulist { Public Static voidMain (string[] args) {intCount = A;//request a linked list of a specified lengthNode n =Yuesefulist.createnodes (count); for(intI=0; i<count;i++) {Node second= N.next;//2nd Onen = n.next.next;//3rd OneSystem. out. println (N.item);//go back to 3rd oneSecond.next= N.next;//let the 2nd next point to the 4th one.n = n.next;//make the 4th one as the 1th } } Public StaticNode Createnodes (intcount) {Node Next=NULL; Node Head=NewNode<string> ("Head",NULL); Node Last= head;//At first , the head and tail are the same element .//new element inserts tail, new element as tail for(intI=1; i<=count; i++) {Next=NewNode<string> ("Num"+i,NULL); Last.next=Next; Last=Next; } Last.next= Head.next;//Finally, the trailing element is linked to the first element inserted.head = Head.next;//remove the head from the beginning, and let the first element of the insert be the head, back out returnHead; } Private Static classNode<e>{E item; Node<E>Next; //node<e> prev;node (E element, node<E>next) { This. Item =element; This. Next =Next; //This.prev = prev; } }}
 Method 2, using LinkedList implementation

Package Com.code.yuesefu;import Java.util.linkedlist;import java.util.List;
/** Joseph Question * 41 persons, circled, from the first person began to count from 1, when met 3 o'clock, that person out. And then the people in the back are going to count from 1 until they get out.

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. *
*/ Public classYuesefu { Public Static voidMain (string[] args) {List List=NewLinkedlist<string>(); Initialize the list of links for(intI=0;i< A; i++){ intA = i+1; List.add ("P"+a); } inti =0; while(List.size () >2) {i= i+2;//move 2 bits each time, if the position exceeds the list length, start againif(I>list.size ()-1) {i= Ilist.size (); } System. out. println (String) list.remove (i)); }
The last two survivors System.
out. println (list.Get(0)); System. out. println (list.Get(1)); }}

Joseph Problem (Java implementation)

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.