Title Description
• 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 IDs. According to the statistics of their posts more than 1/4, you can quickly find them from the list of posts?
First, the solution of ideas
Already familiar with the use of the last Water king method, this time or use "happy to eliminate music," the idea, but the counter from one to three, the definition of the array to represent them, because according to statistics their number of posts more than 1/4, each bucket ID can be offset with the other Navy ID then the last remaining is three water king, Set the first three different Navy ID to the bucket ID, start the offset count, the "Water king" offset after the remainder of 0 is eliminated, with a new Navy ID to continue to cancel, the last remaining is the bucket ID.
Second, the program code
#include"stdafx.h"#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:"<<id[0]<<"\ t"<<id[1]<<"\ t"<<id[2]<<"\ t"<<Endl; return 0;}
Iii. Results
Iv. Experience
As this is the continuation of the water king, and the previous conditions are different, but the solution is the same, for this only need to further improve the algorithm program, to recognize the similarities between them and different points, in the existing basis to expand, in this program should be noted that in the bucket ID initialization, Be careful to select different IDs for the Navy to initialize, then eliminate the count, this condition control to have.
Classroom Practice--Find the Water king continued