Pany package com.com;
import java.util.*;
//application purpose: object-oriented way to realize three take apart; N a child in a circle, from the first child count off (1, 2, 3), when a child at 3 is the loop, it alone until
The class Kid {//children
Int id;
Kid right;//to the right of the child
The Kid left;//the left child
Public void setId (int Id) {//Id assignment, and the right and left initialization
Id=id;
Right=null;
Left=null;
}
}
The class Quan {//circle
int count=0;//record of how many people
Kid first;//record first child
Kid last;//record the last child
Public void deleteKid (Kid a) {//delete child, when children count off for 3
If (count!=0) {
If (count & gt;=1) {
A.r d.light. Left=a. eft. Right;
A. eft. Right=a.r d.light. Left;
A=a.r d.light;
If (Anderson, d==first. Id) first=a.r d.light;
If (Anderson, d==last. Id) last=a. eft.
} else {
The first=null;
The last=null;
}
The count -=1; } the else System. Out. Println (" no one, can't delete ");
}
Public void creatQuan (Kid a) {//the children form a circle
If (count==0) {
The first=a;
The last=a;
}
The else {
Last. Left=a;
A. eft=first;
The last=a;
}
count++;
}
}
Public class Main {
Public static void main (String [] args) {
Scanner in=new Scanner (System. In);
Int n=in nextInt ();
Kid Kid=new Kid ();
Quan Quan=new Quan ();
For (int I=1; I & lt;=n; I++) {
Kid. SetId (I);//to ID assignment
Kid Kid=new Kid ();
Quan. CreatQuan (kid);//add a child
}
N=1;
Kid=quan. First;
While (quan. Count & gt; 1) {
If (n==3) {
Quan. DeleteKid (kid);
N=1;
}
The else {
N=n + 1;
Kid=kid. Right;
}
}
System. The out. Println (kid. Id);
}
}
CodePudding user response:
Want to know the cause of the error and how to modify the code, thank you