One: Programming background
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?
Second: Design ideas
This problem is similar to looking for water King 1, except that the elimination from 1:1 becomes 3:1 elimination, and the other count additions are similar.
Three: Code implementation
//Find the Water King 2//Letter 1405-1 class 20142966 Huang WeipengImportJava.util.Scanner; Public classFind2 { Public Static voidMain (string[] args) { intI,number; System.out.println ("Please enter the total number of posts:"); Scanner in=NewScanner (system.in); number=In.nextint (); intlist[]=New int[number];//define an array of length for the post intcount[]=New int[3];//defines an array of three water king repeating values that are counted separately intmiddle[]=New int[3];//defines an intermediate array that assigns three water kings to each otherSystem.out.println ("Please enter the ID of the post"); Scanner T=NewScanner (system.in); for(i=0;i<number;i++)//Assigning a post ID to an array{List[i]=T.nextint (); } for(i=0;i<3;i++)//initialize a count array{Count[i]=0; } for(i=0;i<number;i++) { if(middle[0]==List[i]) {count[0]++; } Else if(middle[1]==List[i]) {count[1]++; } Else if(middle[2]==List[i]) {count[2]++; } Else if(count[0]==0) {count[0]++; middle[0]=List[i]; } Else if(count[1]==0) {count[1]++; middle[1]=List[i]; } Else if(count[2]==0) {count[2]++; middle[2]=List[i]; } Else{count[0]--; count[1]--; count[2]--; }} System.out.println ("The first water King's ID is:" +middle[0]); System.out.println ("The second water King's ID is:" +middle[1]); System.out.println ("The third water King's ID is:" +middle[2]); }}
Four: Realization
Five: Personal Summary
There are several ways to achieve this problem, but the problem requires a quick find that the algorithm is simple, as long as the number of posts the most ID can be found.
Find the Water King 2