publicclassCount3Quit{publicstaticvoidmain(String[]args){boolean[]arr;arr=newboolean[500];for(inti=0;i1){if(arr[index]=true){now=now+1;if(now==3){arr[index]=false;left=left-1;now=0;}}index=index+1;if(index==arr.length){index=0;}}for(inti=0;i
2 回答
宝慕林4294392
TA贡献2021条经验 获得超8个赞
对应的问题,有对应的数据结构,这个题目出现在很多的招聘面试题目中,是一个经典的应用循环链表的示例。恰好有时间,写了一下代码。废话不多说,上代码,有问题,欢迎讨论:publicclassthirdPersonOutGame{publicstaticvoidmain(String[]args){pointTheLastPerson(init(3),true);}staticPersoninit(inttotalPersonNum){if(totalPersonNum<=0){thrownewIllegalArgumentException("totalpersonnumbercannotbezero.");}Personhead=newPerson(1);Personprev=head;for(inti=2;i<=totalPersonNum;i++){prev=prev.next(newPerson(i));}prev.next(head);System.out.printf("total%dpersonsjointhegame\n",totalPersonNum);returnhead;}staticvoidpointTheLastPerson(Personhead,booleanwhetherPrintQuitOrder){Personprev=head;Personcur=head;intcurIndex=1;if(whetherPrintQuitOrder){System.out.print("personoutorder:");}while(cur.next!=cur){prev=cur;cur=cur.next;if(++curIndex==3){if(whetherPrintQuitOrder){System.out.printf("%d",cur.i);}cur=curPersonOut(cur,prev);curIndex=1;}}if(whetherPrintQuitOrder){System.out.println();}System.out.printf("thelastperson:%d\n",cur.i);}staticPersoncurPersonOut(Personcur,Personprev){returnprev.next=cur.next;}staticclassPerson{inti;Personnext;Person(inti){this.i=i;}Personnext(Personperson){returnthis.next=person;}}}
添加回答
举报
0/150
提交
取消