1. Topics
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?
2. Ideas
Starting from the first ID, and the subsequent post ID comparison, if the same counter plus one, or minus one, minus 0 after the re-assignment
3. Source code
#include "iostream.h" #define MAXSIZE 20void findwater (int id[],int len,int &water) {int counter=0; counter for (int i=0;i<len;i++) {if (counter==0) //If counter is zero {water=id[i]; Record the ID of the current post as the water King suspect counter++;} Else{if (Water==id[i]) //Joshuis suspect the same as the following ID counter++; The counter is recorded, otherwise minus one elsecounter--;}}} int main (int argc, char* argv[]) {int id[maxsize]; Post record form int num,waterking; Number of posts, water King cout<< "Please enter the number of post:";cin>>num;cout<< "Please enter the post ID:"; for (int i=0;i<num;i++) {cin>>id[ I];} Findwater (id,num,waterking);cout<< "Water King is:" <<waterking<<endl;return 0;}
Software Engineering--looking for water king