Find the Water king

Source: Internet
Author: User

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?

Design ideas:

Enter the Post ID record table, starting with the first ID, compare with the following post ID, or minus one if the same counter is added. If the value of the counter is reduced to zero, the current ID is re-selected to start the record comparison. And each time in the same situation, record the number of the same number, while outputting the results of the time to judge whether there is water king. Finally, and output the results.

Source:

#include <iostream>using namespace Std;int findwaterking (int idnum[],int m,int &waterking) {int count=0,            count1=0; Count, same post ID number, water king for (int i=0;i<m;i++) {if (count==0)//Count equals 0 {Waterking=idnum                   [i];                                The current ID is water King count++;           Count plus 1 count1=0;       count1++;           } else//count not equal to 0 {if (Waterking==idnum[i])//current ID equals water king                     {count++;           Count plus 1 count1++;                     } else count--; Count minus 1}} return count1;}

int main () {int idnum[100]; Store post ID int n,waterking,num; Number of posts, water King, same number of posts cout<< "Please enter the number of posts:" <<endl; cin>>n; cout<< "Please enter the Post ID:" <<endl; for (int i=0;i<n;i++) {cin>>idnum[i]; } num=findwaterking (idnum,n,waterking); if (num> (N/2)) cout<< "Water King is:" <<waterking<<endl; else cout<< "No water king!!!" "<<endl; return 0;}


Iv. Personal Summary

This experiment is to find water King's first homework, there are many ways can be achieved, I use is

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.