欢迎来到天天文库
浏览记录
ID:52952196
大小:199.35 KB
页数:3页
时间:2020-04-03
《华为机试-循环出列.pdf》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、计算最后一个出来的人,一圈人1到n,从1开始报数,数到m的人出来,输出最后一个出列者编号packagehuaweiTest.lineUpAndGetOut;publicclassMain{/***@paramargs*/publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubintn=8;intm=4;Lineline=newLine(n);System.out.println(line(line,m).getNum());}
2、privatestaticPointline(Lineline,intm){PointthePoint=line.getHead();//Pointmark;inti=1;while(line.getTrueLength()!=0){thePoint=thePoint.getNext();if(i==m&&thePoint.getState()){line.change(thePoint);i=1;}elseif(i!=m&&thePoint.getState()){i++;}}returntheP
3、oint;}}classLine{privatePointhead;privatePointrear;privateintlength;privateinttrueLength;publicLine(){//TODOAuto-generatedconstructorstubthis.head=newPoint();this.rear=this.head;this.length=0;this.trueLength=0;}publicLine(intlength){this.head=newPoint(
4、);this.rear=this.head;for(inti=1;i<=length;i++){Pointpoint=newPoint(i);point.setNext(this.rear.getNext());this.rear.setNext(point);this.rear=point;}this.rear.setNext(head.getNext());this.length=length;this.trueLength=length;}publicvoidadd(Pointpoint){p
5、oint.setNext(this.rear.getNext());this.rear.setNext(point);this.length++;this.trueLength++;}publicvoidchange(Pointpoint){point.changeState();this.trueLength--;}publicintgetTrueLength(){returntrueLength;}publicintlength(){returnlength;}publicPointgetHea
6、d(){returnhead;}}classPoint{privatePointnext;privatebooleanstate;privateintnum;publicPoint(){//TODOAuto-generatedconstructorstubthis.state=true;}publicPoint(intnum){this.num=num;this.state=true;}publicbooleangetState(){returnstate;}publicvoidchangeStat
7、e(){this.state=false;}publicintgetNum(){returnnum;}publicPointgetNext(){returnnext;}publicvoidsetNext(Pointnext){this.next=next;}}
此文档下载收益归作者所有