Let's have two more interview questions: (Joseph ring)

Source: Internet
Author: User

Question 1: Joseph's ring problem: Java class library only has a cyclic list and no circular linked list.

// Node class

Public class node {
Public int data;
Public node next;

Public node (int I ){
Data = I;
}
}

// Circular linked list
Public class cirlist {
Private node tail;
Private node cur;
Public node head;
Public int size;

// Create a one-way cyclic linked list
Public cirlist (int n ){
Tail = new node (N );
Cur = tail;
For (INT I = n-1; I> 1; I --){
Node node = new node (I );
Node. Next = cur;
Cur = node;
}
Head = new node (1 );
Head. Next = cur;
Tail. Next = head;
Size = N;
}

// Place the element to be deleted in the first position
Public void setheader (INT s ){
Node temp;
For (INT I = 1; I <s; I ++ ){
Temp = head;
Head = head. Next;
Tail = temp;
}
}

// Delete the element at the first position
Public void Delete (){
Head = head. Next;
Tail. Next = head;
Size --;
}}

// Subject class

Import java. Io .*;

Public class Joseph PHUs {

Public static void main (string ARGs []) {
Joseph PHUs JSF = new Joseph PHUs ();
Try {
System. Out. println ("Enter the number of students :");
Int n = integer. valueof (JSF. getstring (). intvalue ();
System. Out. println ("Enter the step :");
Int S = integer. valueof (JSF. getstring (). intvalue ();
Cirlist list = new cirlist (N );
While (list. size! = 1 ){
List. setheader (s );
List. Delete ();
}
System. Out. println (list. Head. data );
} Catch (ioexception e ){
System. Out. println ("Enter data ");
}
}

Public String getstring () throws ioexception {
Inputstreamreader ISR = new inputstreamreader (system. In );
Bufferedreader BR = new bufferedreader (ISR );
String S = Br. Readline ();
Return S;
}}

Question 2: Enter 123456 on the keyboard --> 654321

Import java. Io .*;

Public class porblem2 {

Public static void main (string ARGs []) throws ioexception {
System. Out. println ("Enter the data to be converted :");
Inputstreamreader in = new inputstreamreader (system. In );
Bufferedreader bu = new bufferedreader (in );
String S = Bu. Readline ();
Stringbuffer sb = new stringbuffer (s );
System. Out. println (sb. Reverse ());
}
}

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.