一步一步學習java數組學習(ch5)數3退1記錄最後留下來的在隊列中的位置

來源:互聯網
上載者:User

剛才老師講了java的課程,不過我在視頻中已經學過,就算是當回顧吧。同學們都說java老師講的不好,

我覺得呢,老師是好人,但不是好老師。。或許他那時候當程式員 的時候是比較牛逼的吧。。但願,

只是現在呢。。有點偷懶了吧,講的不太爽。。不過我看了 尚學堂的視頻看到了 第05章_數組_練習8把代碼自己憑記憶

敲了一遍。。以後參考時候用。。我這個不算抄襲吧。。只是記錄一下方便以後用 2011-11-1911:12:18

 1 public class Test{
2 public static void main(String[] args){
3 boolean[] a = new boolean[500];
4 for(int i=0;i<a.length;i++){
5 a[i] = true;
6 }
7
8 int leftcount = a.length;
9 int count = 0;
10 int index = 0;
11 while(leftcount>1){
12 if(a[index]==true){
13 count++;
14 if(count == 3){
15 leftcount--;
16 a[index] = false;
17 count=0;
18 }
19 }
20 index++;
21 if(index == a.length){
22 index = 0;
23 }
24 }
25 for(int i=0;i<a.length;i++){
26 if(a[i] == true){
27 System.out.println(i);
28 }
29 }
30 }
31 }

 利用迴環鏈表來實現的。。

 1 public class Count3Quit1{
2 public static void main(String[] args){
3
4 KidCircle kc = new KidCircle(500);
5 Kid k = kc.first;
6 int countNum = 0;
7 while(kc.count>1){
8 countNum ++;
9 if(countNum==3){
10 countNum = 0;
11 kc.delete(k);
12
13 }
14 k = k.right;
15 }
16 System.out.println(kc.first.id);
17
18 }
19 }
20
21
22 class Kid{
23 int id;
24 Kid left;
25 Kid right;
26 }
27
28 class KidCircle{
29 int count = 0;
30 Kid first,last;
31
32 KidCircle(int n){
33 for(int i=0;i<n;i++){
34 add();
35 }
36
37 }
38
39 void add(){
40 Kid k = new Kid();
41 k.id = count;
42 if(count<=0){
43 k.left = k;
44 k.right = k;
45 first = k;
46 last = k;
47 }else {
48 last.right = k;
49 k.left = last;
50 k.right = first;
51 first.left = k;
52 last = k;
53 }
54 count ++;
55 }
56
57 void delete(Kid k){
58 if(count<=0){
59 return;
60 }else if(count==1){
61 first = last = null;
62 }else{
63 k.left.right = k.right;
64 k.right.left = k.left;
65
66
67
68 if(k == first){
69 first = k.right;
70 }else if(k == last){
71 last = k.left;
72 }
73 }
74 count --;
75 }
76
77 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.