早教吧作业答案频道 -->其他-->
请用JAVA写一个程序:设有n个人站成一排,每一个人有一编号i(1≤i≤n),从左向右“1、2、1、2、...”报数,数到1的人出列,数到2的人立即站到队伍的最右端。报数过程反复进行,直到n个
题目详情
请用JAVA写一个程序:
设有n个人站成一排,每一个人有一编号i(1≤i≤n),从左向右“1、2、1、2、...”报数,数到1的人出列,数到2的人立即站到队伍的最右端。报数过程反复进行,直到n个人都出列为止。已知n个人的原来的顺序,请写出他们的出列顺序。
设有n个人站成一排,每一个人有一编号i(1≤i≤n),从左向右“1、2、1、2、...”报数,数到1的人出列,数到2的人立即站到队伍的最右端。报数过程反复进行,直到n个人都出列为止。已知n个人的原来的顺序,请写出他们的出列顺序。
▼优质解答
答案和解析
public class PersonQueue {
Person head = new Person(0);
Person pointer;
Person last;
public static void main(String[] args) {
new PersonQueue().onStart(5);
}
public void onStart(int count){
if(count <= 0){
throw new IllegalArgumentException();
}
pointer = head;
for (int i = 1; i <= count; i++) {
Person p = new Person(i);
p.setPrevious(pointer);
pointer.setNext(p);
pointer = p;
}
last = pointer;
this.onRun();
}
private void onRun(){
while(head.next != null){
pointer = head.next;
int index = 1;
while(pointer.next != null){
if(index % 2 == 1){
Person p = pointer;
p.count(1);
System.out.println(" i was removed");
pointer = p.next;
p.previous.setNext(pointer);
pointer.setPrevious(p.previous);
}
else if(index % 2 == 0){
Person p = pointer;
p.count(2);
System.out.println(" i was offered the last");
pointer = p.next;
p.previous.setNext(pointer);
pointer.setPrevious(p.previous);
last.setNext(p);
last = p;
last.setNext(null);
}
index++;
}
if(index == 1){
pointer.count(1);
System.out.println(" i was removed");
break;
}
}
}
// ===========================================================
// Inner Class
// ===========================================================
class Person{
int pid = 0;
Person next = null;
Person previous = null;
public Person(int id){
this.pid = id;
}
public int getId(){
return pid;
}
public void count(int num){
System.out.print("my pid is:"+pid+" and i say:"+num +" so ");
}
public void setNext(Person p){
this.next = p;
}
public Person next(){
return next;
}
public Person previous() {
return previous;
}
public void setPrevious(Person previous) {
this.previous = previous;
}
}
}
=========================result==================
my pid is:1 and i say:1 so i was removed
my pid is:2 and i say:2 so i was offered the last
my pid is:3 and i say:1 so i was removed
my pid is:4 and i say:2 so i was offered the last
my pid is:5 and i say:1 so i was removed
my pid is:2 and i say:2 so i was offered the last
my pid is:4 and i say:1 so i was removed
my pid is:2 and i say:1 so i was removed
Person head = new Person(0);
Person pointer;
Person last;
public static void main(String[] args) {
new PersonQueue().onStart(5);
}
public void onStart(int count){
if(count <= 0){
throw new IllegalArgumentException();
}
pointer = head;
for (int i = 1; i <= count; i++) {
Person p = new Person(i);
p.setPrevious(pointer);
pointer.setNext(p);
pointer = p;
}
last = pointer;
this.onRun();
}
private void onRun(){
while(head.next != null){
pointer = head.next;
int index = 1;
while(pointer.next != null){
if(index % 2 == 1){
Person p = pointer;
p.count(1);
System.out.println(" i was removed");
pointer = p.next;
p.previous.setNext(pointer);
pointer.setPrevious(p.previous);
}
else if(index % 2 == 0){
Person p = pointer;
p.count(2);
System.out.println(" i was offered the last");
pointer = p.next;
p.previous.setNext(pointer);
pointer.setPrevious(p.previous);
last.setNext(p);
last = p;
last.setNext(null);
}
index++;
}
if(index == 1){
pointer.count(1);
System.out.println(" i was removed");
break;
}
}
}
// ===========================================================
// Inner Class
// ===========================================================
class Person{
int pid = 0;
Person next = null;
Person previous = null;
public Person(int id){
this.pid = id;
}
public int getId(){
return pid;
}
public void count(int num){
System.out.print("my pid is:"+pid+" and i say:"+num +" so ");
}
public void setNext(Person p){
this.next = p;
}
public Person next(){
return next;
}
public Person previous() {
return previous;
}
public void setPrevious(Person previous) {
this.previous = previous;
}
}
}
=========================result==================
my pid is:1 and i say:1 so i was removed
my pid is:2 and i say:2 so i was offered the last
my pid is:3 and i say:1 so i was removed
my pid is:4 and i say:2 so i was offered the last
my pid is:5 and i say:1 so i was removed
my pid is:2 and i say:2 so i was offered the last
my pid is:4 and i say:1 so i was removed
my pid is:2 and i say:1 so i was removed
看了 请用JAVA写一个程序:设有...的网友还看了以下:
脉冲波形的产生与变换1、10、555定时器:A、是一个包含模拟和数字电路的中规模集成芯片.B、其4 2020-04-07 …
按以下要求填空1;quiet()写出副词2;careful()写出副词3;pick()写出过去式4 2020-04-27 …
将下面的复句改写成一将下面的复句改写成一个反问句.要求:不改变句子原意,词语可以适当增删,词序可以 2020-05-13 …
麻烦老师解答:写出下列名词的复数形写出下列名词的复数形式.1.schoolbus2.pencil- 2020-05-14 …
library(写复数) mouse(写复数) blue同类词) I(同义词) have(第三人称 2020-05-17 …
某STP网络从链路故障中恢复时,端口收敛时间超过30秒,处理该故障的思路不包括:( )。A. 确认对 2020-05-26 …
关于期末打算的作文500字以上(本人数学成绩不好,应该怎样复习,要写清楚,还要从哪些方面复习,写清 2020-06-30 …
信访答复由谁写?如:李某到信访办提出生活困难,请求民政局补助.这个信访件的答复应该由民政局书面答复 2020-07-16 …
同分异构体的书写口诀,帮忙解析一下成直链,一条线;摘一碳,挂中间;往边排,不到端;摘两碳,成乙基;二 2020-11-30 …
(英文作文)失物招领的回复假如你(Tom)的绿色手表丢了,而你的朋友Frank在失物招领处的柜子里看 2020-12-21 …