Homework--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:

(1) Enter the Post ID record form

(2) Start with the first ID, and follow the post ID to compare, if the same counter plus one, or minus one. If the value of the counter is reduced to zero, the current ID is re-selected to start the record comparison.

(3) Output results

Source:

#include <iostream>using namespacestd;intFindwaterking (intIdnum[],intMint&waterking) {   intCount=0, count1=0;//count, same post ID number, water king    for(intI=0; i<m;i++)   {       if(count==0)//count equals 0{waterking=idnum[i];//The current ID is water Kingcount++;//count plus 1count1=0; Count1++; }       Else                                  //count is not equal to 0       {           if(Waterking==idnum[i])//current ID equals water King{Count++;//count plus 1count1++; }           ElseCount--;//count minus 1       }      }   returnCount1; }intMain () {intidnum[ -];//Store Post ID  intN,waterking,num;//number of posts, water King, number of same postscout<<"Please enter the number of posts:"<<Endl; CIN>>N; cout<<"Please enter the Post ID:"<<Endl;  for(intI=0; i<n;i++) {cin>>Idnum[i]; } num=findwaterking (idnum,n,waterking);if(Num> (n/2)) cout<<"The Water king is:"<<waterking<<Endl;Elsecout<<"no water king!!! "<<Endl;}


Iv. Personal Summary

Every time you write a program, you have to test the boundary, or it will go wrong.

Group members: Yang Tao http://www.cnblogs.com/GloryYT/

Homework--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.