對應(yīng)的問題,有對應(yīng)的數(shù)據(jù)結(jié)構(gòu),這個題目出現(xiàn)在很多的招聘面試題目中,是一個經(jīng)典的應(yīng)用循環(huán)鏈表的示例。恰好有時間,寫了一下代碼。廢話不多說,上代碼,有問題,歡迎討論: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;}}}