Experimental topics
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?
Experimental ideas
Just like the last time, only the counter is changed from one to three, the definition array represents them, because according to statistics their number of posts more than 1/4, each bucket ID can be offset with other Navy ID to cancel the last is three water king, the first three different Navy ID to the bucket ID, start to offset the count, "Water king "The remainder of the 0 is eliminated with a new Navy ID, and the last remaining is the bucket ID."
Code implementation
1#include <iostream>2 using namespacestd;3 4 intMain ()5 {6 //first assume that the user's ID list is an array7 intid[ -]={1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,1,2,3};8 intshuiw=id[0];//default water king Absalom first ID9 intLength= -;Ten intidnum[3]={0,0,0}; One intid[3]={0,0,0}; Acout <<"The Water King ID list is"<<Endl; - for(intI=0; i<length;i++)//Output ID list - { thecout<<id[i]<<" "; - } -cout<<Endl; - for(intI=0; i<length;i++) + { - if(idnum[0]==0&& id[i]!=id[1] && id[i]!=id[2])//The first king of water + { Aidnum[0]=1; atid[0]=Id[i]; - } - Else if(idnum[1]==0&& id[i]!=id[0] && id[i]!=id[2])//The second king of water - { -idnum[1]=1; -id[1]=Id[i]; in } - Else if(idnum[2]==0&& id[i]!=id[0] && id[i]!=id[1]) to { +idnum[2]=1; -id[2]=Id[i]; the } * Else if(id[i]!=id[0] && id[i]!=id[1] && id[i]!=id[2])//offset each other, and finally offset to zero elimination, replace the new ID as the water king $ {Panax Notoginsengidnum[0]--; -idnum[1]--; theidnum[2]--; + } A Else if(id[i]==id[0]) the { +idnum[0]++; - } $ Else if(id[i]==id[1]) $ { -idnum[1]++; - } the Else if(id[i]==id[2]) - {Wuyiidnum[2]++; the } - } Wucout<<"The first water King's ID is:"<<id[0]<<Endl; -cout<<"The second water King's ID is:"<<id[1]<<Endl; Aboutcout<<"The third water King's ID is:"<<id[2]<<Endl; $ return 0; -}
Run
"Three water Kings" in class practice