Looking for the water king

Source: Internet
Author: User

First, the topic 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? requires the design idea, code implementation, implementation, personal summary in the form of blog post. (Due date 2015-4-22 night 18:00)second, the design ideaclass with sorting algorithm to arrange the ID table in order, according to the water King ID more than the total number of stickers in half, then in the ordered table of the N/2 item must be water King ID This idea can be the King ID, the complexity of the algorithm is O (n*n), but the teacher requires time complexity O (n), so you can traverse the ID table 22 adjacent ID For comparison, if the difference is deleted, then in the remaining ID table water King still more than half of the total ID number, repeated this process, the total number of IDs in the list is reduced, the last remaining is definitely the water King ID, these unordered ID numbers can be expressed in the array arr. Third, the Code 
#include <iostream.h>void main () {int arr[11]={1,2,2,3,4,2,2,5,3,2,2};//assumed ID number int shuiwang = 0;  int count=0;  Mark for (int i=0;i<11;i++) {  if (count = = 0) {  Shuiwang = arr[i];  Count = 1;  }  else{  if (Shuiwang = = Arr[i])  count + +;  Else   count--;  }  } cout<< "Water King's ID is" <<SHUIWANG<<ENDL;}

Four

V. Summary

The subject of this experiment is the water Wang FA post ID number more than half of the ID number, so this is the breakthrough point of this problem, for time complexity, good design ideas can greatly reduce the complexity of time, such as this problem can be the time complexity from O (n*n) to O (N).

Looking for 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.