Software Engineering--Seeking Water King (cont.)

Source: Internet
Author: User

First, the topic

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 ID. According to statistics their number of posts more than 1/4, you can quickly find them from the list of posts

Second, design ideas

Referring to the solution to the original problem, if you delete 4 different IDs each time (regardless of whether it exceeds the total 1/4 id), then the percentage of the remaining ID list is greater than 1/4 for the original number of IDs that are larger than 1/4. You can get the answer to the question by constantly repeating the process and reducing the total number of IDs. Method: Record three candidate IDs with Candidate[3], record their cumulative number with count[3], and then traverse the entire ID list, each processing an ID, if the same as one in Candidate[i], then count[i]++, if it is different from three, The description found four different IDs, the three count[i]--, also equivalent to "delete four separate IDs", if a count[i]==0, then update.

Third, the source code

1  PackageCom.java.lianxi;2 3  Public classLianxi8 {4      Public Static voidMain (String arg[]) {5         intid[]={1,2,1,1,2,3,2,3,3,4,4};6 Find (ID);7     }8      Public Static voidFind (intid[])9     {  Ten         intI,n=id.length; One         intntimes[]=New int[3];  A         intcandidate[]=New int[3];  -Ntimes[0]=ntimes[1]=ntimes[2]=0;  -Candidate[0]=candidate[1]=candidate[2]=0;  the          for(i = 0; i < N; i++)   -         {   -             if(id[i]==candidate[0])   -             {   +ntimes[0]++;  -             }   +             Else if(id[i]==candidate[1])   A             {   atntimes[1]++;  -             }   -             Else if(id[i]==candidate[2])   -             {   -ntimes[2]++;  -             }   in             Else if(ntimes[0]==0)   -             {   toNtimes[0]=1;  +candidate[0]=Id[i];  -             }   the             Else if(ntimes[1]==0)   *             {   $Ntimes[1]=1; Panax Notoginsengcandidate[1]=Id[i];  -             }   the             Else if(ntimes[2]==0)   +             {   ANtimes[2]=1;  thecandidate[2]=Id[i];  +             }   -             Else   $             {   $ntimes[0]--;  -ntimes[1]--;  -ntimes[2]--;  the              }   -         } Wuyi          for(i=0;i<3;i++) the         { -System.out.println ("Water King" + (i+1) + "ID is" +candidate[i]); Wu         } -         }  About  $}

Four

V. Summary of the Experiment

This time to find the expansion of the king, and the original idea of the water king is the same, are the use of mutual elimination method, but from 22 to four four of the elimination of each other. When we solve this kind of expansion problem, we must think on the basis of the original problem, so that the thinking will be clearer and the efficiency of solving the problem will be higher.

Software Engineering--Seeking Water King (cont.)

Related Article

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.