Looking for a small aqua king

Source: Internet
Author: User

Looking for an extension to the "Water King" issue: "Super Water King" is gone. Statistical results show that there are 3 posts a lot of IDs, their number of postings are more than the total number of post n 1/4. Can you quickly find their ID from the list of post IDs?

(i) Analysis of the problem:

If you delete four different IDs at a time (regardless of whether or not they contain more than 1/4 of the total number of posts), then in the remaining list of IDs, the proportion of the original posting ratio greater than 1/4 is still greater than 1/4, which can be repeated by repeating the process, Reduce the total number of IDs in the ID list (into smaller issues) to get answers to the questions

(b) Code implementation:

PackageFind the king of the waters;PublicClassXiaoshuiwang {Staticvoid Find (int id[],int N,IntCandidate[]) {int ntimes[]={0,0,0};Forint i=0;i<n;i++) {if (id[i]==candidate[0]) {ntimes[0]++;}Elseif (id[i]==candidate[1]) {ntimes[1]++;}Elseif (id[i]==candidate[1]) {ntimes[2]++;}Elseif (ntimes[0]==0) {Ntimes[0]=1; candidate[0]=Id[i]; }Elseif (ntimes[1]==0) {Ntimes[1]=1; candidate[1]=Id[i]; }Elseif (ntimes[2]==0) {Ntimes[2]=1; candidate[2]=Id[i]; }Else{ntimes[0]--; ntimes[1]--; ntimes[2]--; }} System.out.println ("Three qualifying IDs are respectively");for (int i=0;i<3;i++ public static void< Span style= "color: #000000;" > Main (String args[]) {int id[]={1,1,1,2,2,2,3,3,3,1,2,3,1,2,3,6}; int n=16int candidate[] = { -1,-1,-1); for (int i=0;i<n;i++  

(iii) Experiments:

Looking for a small aqua king

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.