Step by step learning Java array Learning (CH5) count 3 return 1 record the last position left in the queue

Source: Internet
Author: User

I spoke about the Java course just now, but I have already learned it in the video. Let's review it. The students all said that the Java teacher did not speak well,

In my opinion, the teacher is a good person, but not a good teacher .. Maybe he was a programmer at that time .. Hopefully,

But now .. It's a bit lazy. It's not a good talk .. However, after reading the video of Shang xuexiang, I saw Chapter 2 _ arrays _ exercise 8. Remember the code by yourself.

I typed it again .. It will be used for future reference .. I am not a plagiarize .. Just record it and use 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 }

This is achieved by using the loop linked list ..

 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 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.