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