1. 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? (Refer to Core code)
With the development of the Forum, the administrator found that the water king did not, but the statistical results show that there are three posts a lot of ID. According to the statistics of their posts more than 1/4, you can quickly find them from the list of posts?
2. Design ideas.
By eliminating the method of implementation, respectively, three consecutive IDs to counter[0],counter[1],counter[2], and then detect the fourth number, if it is different from the previous three, each counter minus one, otherwise the same counter plus one. The last ID has been detected.
3. Code.
#include <iostream.h>#include<stdlib.h>intMain () {intlength; intidnum[3]={0,0,0}; intid[3]={0,0,0}; cout<<"Please enter the total number of posts:"<<Endl; CIN>>length; int* curid=New int[length]; cout<<"Please enter the ID list of the Navy"<<Endl; for(intj=0; j<length;j++) {cin>>Curid[j]; } for(intI=0; i<length;i++) { if(idnum[0]==0&& curid[i]!=id[1] && curid[i]!=id[2]) {idnum[0]=1; id[0]=Curid[i]; } Else if(idnum[1]==0&& curid[i]!=id[0] && curid[i]!=id[2]) {idnum[1]=1; id[1]=Curid[i]; } Else if(idnum[2]==0&& curid[i]!=id[0] && curid[i]!=id[1]) {idnum[2]=1; id[2]=Curid[i]; } Else if(curid[i]!=id[0] && curid[i]!=id[1] && curid[i]!=id[2]) {idnum[0]--; idnum[1]--; idnum[2]--; } Else if(curid[i]==id[0]) {idnum[0]++; } Else if(curid[i]==id[1]) {idnum[1]++; } Else if(curid[i]==id[2]) {idnum[2]++; }} cout<<"three bucket IDs are:"; cout<<id[0]<<" "<<id[1]<<" "<<id[2]<<" "<<Endl; return 0;}
4. Run the results.
5. Experience.
Through test instructions, the code algorithm is implemented by eliminating the method.
Class practice-Find water King continued