ImportJava.util.Scanner;classperson{String name; intseqno; Person (String _name,int_seqno) {Name=_name; Seqno=_seqno; }} Public classjosephcircle { Public Static voidMain (string[] args) {//TODO auto-generated Method StubScanner Scanner =NewScanner (system.in); System.out.println ("------Please enter the total number------"); intTotalnum =Scanner.nextint (); System.out.println ("----Please enter the size of the count----"); intOutnum =Scanner.nextint (); String[] Name=NewString[totalnum]; System.out.println ("----Please enter the" +totalnum+ "person's name and return to the end----"); for(inti = 0; i < totalnum+1; i++) {String str=Scanner.nextline (); if(i = = 0) Continue; Name[i-1] =str; } scanner.close (); Josecicircle (Totalnum, Outnum, name); } Private Static voidJosecicircle (intTotalnum,intOutnum, string[] name) {person[] PS=NewPerson[totalnum]; for(inti = 0; i < Totalnum; i++) {person P=NewPerson (Name[i], i+1); Ps[i]=p; } intPre = 0; intCurrentnum =Totalnum; while(Currentnum > 0) {Pre= (pre + outNum-1)%Currentnum; System.out.println ("----" +ps[pre].seqno + "is out----"); PS=Adjust (PS, pre, currentnum); Currentnum--; } } Private StaticPerson[] Adjust (person[) PS,intPreintcurrentnum) { //TODO auto-generated Method Stub for(inti = Pre; i < currentNum-1; i++) {Ps[i]= Ps[i+1]; } returnPS; }}
This is the use of an array to implement the Joseph Ring, used in the object array Person[],person class with its own numbering.
Welcome to comment, communicate with each other!
Reprint please specify the source! Thank you
Joseph ring Java Object Array implementation