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?
Design thought: This time the water king has three, so I chose to exclude one by one to exclude, assuming that the first ID is water king, with it and then the other ID to compare, the number of occurrences of statistics, and finally take this ID all out, put in another array (the idea of stack into the stack). Then cycle through the process until all the water kings find out.
Packageshuiwang2;ImportJava.util.*; Public classFind_shuiwang { Public Static voidMain (string[] args) {//TODO auto-generated Method Stubfind_function function =Newfind_function (); Scanner SCA1=NewScanner (system.in); Scanner SCA2=NewScanner (system.in); System.out.println ("Enter Number of posts:"); intn=Sca1.nextint (); String post[]=NewString[n]; inti; System.out.println ("Enter the Post ID:"); for(i=0;i<n;i++) {Post[i]=Sca2.nextline (); } String shuiwang[]=Function.find (post, n); System.out.println ("Water King ID is:"); for(i=0;i<3;i++) {System.out.println (Shuiwang[i]+" "); } }}Find_shuiwang
Packageshuiwang2; Public classfind_function {string[] Find (string[] post,intN) {intI,j = 0; intNum_find=0;//find several of the same IDs intnum_mark=0;//tag has several IDs into the stack intNum_shuiwang=0; String shuiwang[]=NewString[3]; String zhan[]=NewSTRING[N+1];//build a stack-up ID while(num_shuiwang<3) {//when the water king hasn't been looking all the time before the cycleSHUIWANG[NUM_SHUIWANG]=POST[0];//make the first id a water king for(i=0;i<n-num_mark;i++)//22 Contrast { if(Shuiwang[num_shuiwang].equals (Post[i])) {Zhan[num_mark]=Post[i]; Num_find+=1; for(j=i;j<n-num_mark-1;j++)//move forward Array{Post[j]=post[j+1]; } Num_mark+=1; } } if(Num_find >= N/4) {Num_shuiwang+=1; Num_find= 0; } Else{Num_find= 0; } } returnShuiwang; }}find_function
Results:
Software engineering Exercise--Find the Water King 2