Software engineering work-Finding water king

Source: Internet
Author: User

Topic:

• Three people have designed a irrigation forum. students at the school of information are fond of exchanging water on top, legend has a "water king" on the forum, he not only likes to post, it will also reply to each post sent by other IDs. 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? 1. Design idea: Delete Two different IDs each time, in the remaining ID, the original maximum frequency ID appears as much as more than 50%, repeat this process, the last remaining will be all the same ID, that is, water king.

2. The code is as follows:

#include<iostream> using namespace std; int FindID( int num[],  int n) {      int index = 0,count = 0;      for (int i = 0; i < n; i++)      {          if (count == 0)          {              index = num[i];              count = 1;          }          else          {              if (index == num[i])              {                  count++;              }              else              {                  count--;              }          }      }      return index; }   int main() {      int num, shui;      int arr[] = {2,2,3,3,5,2,8,2,2,12,2,2,3,9,2,2,2,7,4,2};      num =  sizeof (arr)/ sizeof ( int );      shui = FindID(arr, num);      cout<< "水王是" <<shui<<endl; }3. Results:

4. Personal Summary:

There is no good abstract problem, in fact, the original problem can be abstracted to give you an array, there are more than half of the number is the same, find out the number of the most occurrences, the algorithm is not very good design, the first thing is to sort and then output the median, and finally in the inspiration of others to complete.

Software engineering work-Finding water king

Related Article

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.