The question of finding water king

Source: Internet
Author: User

1. Title:

A three-person line designed a forum for irrigation. 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?

2. Design ideas:

Using the inverse idea of the elimination of music, the adjacent two different IDs in turn eliminated, because the water king ID more than half of the total, so the final remaining must be the water King ID.

3. Source code:

1#include <iostream.h>2#include <stdlib.h>3 intId=0; 4 intCount=0; 5 voidFindid (intArr[],intN)6 { 7      for(intI=0; i<n;i++)   8     {   9         if(count==0)  Ten         {    Onecount++; AId=Arr[i]; -         }    -         Else   the         {    -             if(id==Arr[i]) -count++;  -             Else          +count--;  -         }    +     }    A }    at intMain () - {  -     intN;  -     int*arr=New int[];  -cout<<"Please enter the total number of posts:";  -Cin>>N; incout<<"Please enter the ID of each navy:"<<Endl; -      for(intI=0; i<n;i++)  to     {  +Cin>>Arr[i]; -     }  the Findid (arr,n); *cout<<"the water King's ID is:"<<Id<<Endl;; $     return 0;Panax Notoginseng}

4. Results:

5. Experience:

This programming practice is also heavy in the design idea, get the topic or the old idea, first from the most stupid method to start, and then to achieve the optimization of the method, the final time to achieve the complexity of the requirements, this for the future programming development has a great help.

The question of finding water 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.