Software engineering work--Find the Water king continued

Source: Internet
Author: User

Title Requirements:

• Three people have designed a irrigation forum. Information college students like to exchange irrigation above, legend in the forum there is a "water king", he not only likes to post, but also reply to other ID issued by each post. The "Water King" has been rumored to have posted more than half the number of posts. • If you have a list of posts (including replies) for the current forum, and the ID of the author of the Post is in it, can you quickly find the legendary water king? ( refer to Core code ) • With the development of the Forum, the administrator found that the water king did not, but the statistical results show that there are three posts a lot of IDs. According to statistics their number of posts over 1/4, you can quickly find them from the list of posts ? Implementation ideas:1. Abstract the problem, there is currently a set of data, which is greater than or equal to 4 per 1 of the data is a definite value, and such a determination of the value of three, the rest of the value of less than One-fourth is not repeated.

2. This is good to do, or according to the first time to find the idea of water king to be able to, but this time the return value is three, need to use the array to solve,ntimes 3 elements corresponding to the current traversal of the number of 3 IDs appear. If there is an ID in the traversal that differs from these 3 current IDs, we determine whether the current 3 ID has a ntimes of 0, and if so, the new traversal ID is replaced with 1 for its traversal number (that is, ntimes minus 1), if the current 3 ID ntimes is not 0, The ntimes of 3 IDs minus 1, which is the key to solving this problem. Because the non-water king ID is dissatisfied with the total number of posts of 1/4, and the same idea, the traversal ID and the current 3 ID is different, it is offset (that is, 3 of the current ID of the Ntimes value minus 1), the final remaining 3 current ID will always be 3 more than 1/4 water King ID.

Code implementation:

1  Public classFind {2     Static int[] id=New int[100];3     intRand ()//generate a random number within 0-14     {5Java.util.Random random=NewJava.util.Random ();//Defining Random Classes6         intResult=random.nextint (4);//returns an integer in the [0,4] collection, noting that 4 is not included7         returnresult;8     }9     voidFIRSTC ()//Initializes an array of arrays;Ten     { One          for(inti=0;i<100;i++) A         { -             if(Rand () ==0) -             { theid[i]=50;//the ID of the water king number one -             } -             Else if(Rand () ==1) -             { +id[i]=60;//ID of Water King No. second -             } +             Else if(Rand () ==2) A             { atid[i]=70;//ID of Water King No. third -             } -             Else -             { -id[i]=i; -             } in              -         } to      +     } -     voidout () the     { *System.out.println ("The following are the ID sheets of the water stickers"); $          for(inti=1;i<=id.length;i++)Panax Notoginseng         { -System.out.print (id[i-1]+ ""); the             if(i%10==0) +             { ASystem.out.print ("\ n"); the             } +         } -     } $ /*int Serch ()//Thought realization part $     { - int candidate=50; - int ntimes,i; the For (i = Ntimes = 0;i<id.length;i++) { - if (ntimes = = 0) {Wuyi candidate = Id[i]; the ntimes = 1; -           } Wu else{ - if (candidate = = Id[i]) About ntimes + +; $ Else - Ntimes--; -           } -         } A return candidate; +     }*/ the      Public Static int[] Serchs (int[] ID) -     { $          the                 if(ID = =NULL)return NULL;  the                 intcandate0= 0;  the                 intcandate1= 0;  the                 intcandate2= 0;  -                 intTimes0 = 0;  in                 inttimes1 = 0;  the                 intTimes2 = 0;  the                 intLen =id.length;  About                  for(intI =0;i<len;i++){   the                     if(times0==0&&id[i]!=candate1&&id[i]!=candate2) {   thecandate0=Id[i];  thetimes0++;  +                     }   -                     Else if(times1==0&&id[i]!=candate0&&id[i]!=candate2) {   thecandate1=Id[i]; Bayitimes1++;  the                     }   the                     Else if(times2==0&&id[i]!=candate0&&id[i]!=candate1) {   -Candate2=Id[i];  -times2++;  the                     }   the                     Else if(id[i]!=candate1&&id[i]!=candate0&&id[i]!=candate2) {   thetimes0--;  thetimes1--;  -times2--;  the                     }   the                     Else if(id[i]==candate0) thetimes0++; 94                     Else if(id[i]==candate1) thetimes1++;  the                     Else if(id[i]==candate2) thetimes2++; 98                 }   About                int[] result ={Candate0,candate1,candate2};  -                 returnresult; 101             }  102           103@SuppressWarnings ("Static-access")104      Public Static voidMain (string[] args) { theFind A =NewFind ();106 A.FIRSTC ();107 a.out ();108         int[] results=New int[3];109results=A.serchs (ID); theSystem.out.println ("Three water Kings ' IDs are respectively");111          for(inti=0;i<3;i++) the         {113System.out.println ("First" + (i+1) + "Water King ID is" +results[i]); the         } the          the     }117 118}

Operation Result:

Modify the Water King ID to run the program again and observe the results:

Operation Result:

Experience:

1. Thought is very important,

2. When faced with repeated thinking after really do not understand the problem is the first reaction should be "Baidu" of course, the first problem to see will be in hand in the homework, learned is their own, learning ability is also very important!

Software engineering work--Find the Water king continued

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.