Joseph Ring Code Implementation in Java

Source: Internet
Author: User

Problem Prototypes:

Legend has it that in a long time ago, a plane carrying N-man had malfunctioned and made a landing on a deserted island. After the plane was completely scrapped, these people used the wreckage of the plane to build a boat that could only hold one person, so how to determine which of the N individuals is eligible to go aboard? thus, the N-person adopted the following way to solve this Dilemma.

Problem Description:

Suppose there are n individuals in a circle, and each has a unique sequential number from 1 to N. next, the number of people numbered 1 starts to count off. The man with the m number exits the Circle. Then by the next person from 1 to start a count, so the cycle, then finally left to the number of the person?

Import java.util.Scanner;

public class out{

public static void main (String Args[])

{

Out Out=new out ();

Receive user input, get the number of people in the dequeue n

Scanner sc=new Scanner (system.in);

System.out.println ("please Enter the total number of participants:");

int n= Sc.nextint ();

Receive user input, get the number of people in the dequeue m

System.out.println ("please Enter the number of the Dequeue person:");

int M=sc.nextint ();

Creates an array of Boolean types with a total number of characters

Boolean rs[]=new boolean[n];

Initializes a Boolean array with the initial value true

For (int I=0;i<n;i++) {

rs[i]=true;

}

int n=n;//remaining Number of people

Number of int m=0;//

Out.save ();

Print out the number of the last person left

For (int K=0;k<n;k++) {

If (rs[k]) {

System.out.println ("remainder" + (k+1) + "personal");

Break

}

}

}

public void Save () {

While (n>1)

For (int J=0;j<n;j++) {

If (r[j]) {

m++;

If (m==m) {

m=0;

rs[j]=false;

n--;

}

}

}

}

Joseph Ring Code Implementation in Java

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.