日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

java例子:数组 数3退1

發(fā)布時(shí)間:2023/12/10 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java例子:数组 数3退1 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

500個(gè)人圍成一個(gè)圈子,數(shù)夠3人,就退出1個(gè),問最后剩下的是幾號(hào)?

????????檢驗(yàn)先有5個(gè)人,應(yīng)該留下第4個(gè)人,由于是數(shù)組,所以第四個(gè)人的下標(biāo)是3.

/*achieve the funtion :count 3 kids, the quit the third kid use the object thinking */public class Test{public static void main(String[] args) {KidCircle kc = new KidCircle(5);int countNum = 0;Kid k = kc.first;while(kc.count > 1){countNum ++;if(countNum ==3){countNum =0;kc.delete(k);}k = k.right;}System.out.println(kc.first.id); } }/* define class Kid*/ class Kid{int id;Kid left; //left is means the current kids'left kid Kid right; //right is means the current kids'right kid }/* define class KidCircle*/ class KidCircle{int count = 0; //mean the circles'numberKid first,last; //define the circles' first kid and last kid.KidCircle(int n){//Constructor :new n numbers kid circlefor(int i = 0;i<n;i++){add();}}/*add a kid in the last kids'right*/void add(){/*first: new a kid;id = count*/Kid k = new Kid();k.id = count;/*if no kid in the circle ,the new kid is the first kid and is the last kid*/if(count <=0){first = k;last = k;k.left = k;k.right = k;}/*if has some kids in the circle,the new kid will stand at the last kids'right,and the new kids'left is the last kid. another: the new kids'right is the first kids'lest*/else{last.right = k;k.left = last;k.right = first;first.left = k;last = k;}count ++;}void delete(Kid k){if(count <= 0){return;}else if(count == 1){first = last =null;}else{k.left.right = k.right;k.right.left = k.left;if(k == first){first = k.right;}else if(k == last){last = k.left;}}count --;} }

總結(jié)

以上是生活随笔為你收集整理的java例子:数组 数3退1的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。