Find the Water king

Source: Internet
Author: User

Experimental topics

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?

Experimental ideas

Since the water King's post and the number of replies are more than half, then we just need to sort all the ID number in the array, and then find the ID in the middle of the array must be the water King ID, but this method is to be optimized. Put all the IDs according to the order of posting and replies in the array, and then call a loop to start the traversal, two adjacent to the comparison, if not the same will be eliminated, in the array of numbers also changed along with it, know that in the end will find the water king.

Experiment Code

//Letter 1201-2 Division new Red#include"stdafx.h"#include"iostream"using namespacestd;intMain () {inta[Ten]={1,3,2,3,2,3,1,3,3}; intresult = a[0]; intTimes =1;  for(inti =1; I <Ten; i++)    {        if(A[i] = =result) times++; Else Times--; if(Times = =0) {result=A[i]; Times=1; }} cout<<"Water King Absalom:"<<result<<Endl; return 0;}

Experiment Experience:
This experiment is very interesting, not very difficult, but there are very interesting points of thinking, like the problem.

Find the 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.