Java using cyclic chain table structure to solve Joseph problem _java

Source: Internet
Author: User

This paper illustrates the method of solving Joseph problem by using cyclic chain table structure in Java. Share to everyone for your reference. The specific analysis is as follows:

This is the first test of the Java exam, for the students who have not seen the linked list will not do, now look back, it is not difficult.

Joseph Question:
there are n individuals, whose numbers are 1,2,3,...,n respectively. The n men are in a circle in order. Now given S and D, starting from the beginning of the number of individuals from 1, count to D of the person out of the column, and then from the next person started again from 1 to start off, the number of people in the D and out, so cycle, until the end of all the column. A node class is defined, and the problem of Joseph is solved by cyclic chain table structure.

The following Java version of the answer:

Copy Code code as follows:
Import Java.util.Scanner;
public class Linknode {//One-way linked list node class
public int data; Store node values
Public Linknode Next; A reference that holds the node value

public Linknode (int k) {//constructor method, node with value K
data = k;
next= null;
}
}
Class josephus{
public static void Printjosephus (int n,int s,int d) {
int i=1; Create a loop list that is long n
Linknode Q,tail;

Linknode head = new Linknode (i);
Head.next = head;
tail = head; The first node, the tail and the head together

while (I<n) {
i++;
Q = new Linknode (i); Add a new node
Q.next = head; The reference to the node points to the header
Tail.next = q; A reference to the last element points to the Q
tail = q; So the last element is Q.
}

int j= 0; Start off from S, then output the number of the column.
Linknode p = head; Counting beginnings
while (j<s-1) {
j + +;
p = p.next;
}
while (P.next!= p) {
j = 1;
while (j<d-1)//counting Start point
{
j + +;
p = p.next;
}
System.out.print (P.next.data + ""); Output the node number of the column
P.next = P.next.next;
p = p.next; Keep pointing to the next node
}
System.out.print (P.data);
}

public static void Main (string[] args) {
Scanner input = new Scanner (system.in);
int n = input.nextint ();
int a = Input.nextint ();
int b = Input.nextint ();
Josephus.printjosephus (n, a, b);
}
}

I hope this article will help you with your Java programming.

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.