The beauty of programming-"Water king" Water paste calculation

Source: Internet
Author: User

Title Requirements:

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? Idea: We notice in this ID list that the water King's ID is half of the number of posts that has become a breach of the question, just as you can choose three people to vote, and only those three people in the election, and if you each vote, it means you gave up your choice. The water king has the highest number of votes, and when he subtracts the votes of others, he is still positive, and he wins. Source:
#include <iostream.h>int find (int *id, int N)  {      int ntimes = 0;      int i;      int people;      for (i = 0; i < N; i++)      {          if (Ntimes = = 0)          {              people = id[i];              ntimes++;          }          else          {              if (people = = Id[i])                  ntimes++;              else                  ntimes--;          }      }        return people;  } void Main () {    int a[15]={5,6,8,2,4,9,7,5,7,6,5,2,5,1,5};int record=find (a,15);cout<< "Water King id" << "\ T" <<record<<endl;}

Results:

The beauty of programming-"Water king" Water paste calculation

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.