A few days ago to know CCF computer professional certification exam, I feel like some of the higher gold content than the soft test, went to understand a little, do a mock examination papers in the "largest number of occurrences number" This question, I answer algorithms and algorithms official different, personally I feel a little better feel official, not so complicated, that first glance might feel a little difficult to understand, I'll do a parsing official answer below, will finally put their own code.
Do mock examination papers must first visit the official website: https: //passport.ccf.org.cn/sso/platform,CSP certification exam registration → Exam (recommended to open the computer, the phone can not see the mock examinations)
Simulation questions answers directly in the official website to download
Topics are as follows:
The second line has the input n integers S . 1 , S 2 , ..., S n (S. 1 ≤ I ≤ 10000, ≤ I ≤ n. 1). Adjacent number separated by a space.
10 1 10 20 30 20
Official correct codes and analysis:
. 1 Import Classes in java.util *. ; 2 public class Main { . 3 public static void main (String [] args) { . 4 new new Main () run ();. // call Main class run () function . 5 } . 6 public void RUN () { . 7 Scanner FIN = new new Scanner (the System.in); . 8 int N = fin.nextInt (); . 9 int [] COUNT = new new int [10001]; // create an array of length count 10001 of 10 for (int I = 0; I <N; ++ I) . 11 { 12 is ++ COUNT [fin.nextInt ()]; // set one cycle, we entered integer as a subscript of the array, the number of the next mark plus a number of elements in the 13 is } 14 int maxCount = -1; // set identifier of a record number 15 int Result = 0; // set a record of the most current number of times of occurrence identifier 16 for ( int I = . 1; I <= 10000; ++ I) . 17 { 18 is IF (count [I]> maxCount) // traverse count array, if the current record number is greater than maxCount element, the element will be recorded and the index number and the presence of current maxCount result in 19 { 20 = maxCount COUNT [I]; 21 is Result = I; 22 is } 23 is } 24 System.out.println (Result); // final output of the most frequent number 25 } 26 }
Lines 10-13 of the code input by the number as a subscript of the array element count, for each input, it will count the number of array elements in the target number plus one, and the default value of the java array type int is 0, an example:
Now n = 5, to be input respectively an integer of 12,34,2,12,4. The operating system is: ++ count [12], ++ count [34], ++ count [2] ,, ++ count [12] ,, ++ count [4],
So count [12] = 2, count [34] = 1, count [2] = 1, count [4] = 1, and the number is not entered is: count [45] = 0, count [265] = 0 , count [8] = 0 ......
Finally, you can only compare a maximum number of times appear again count by traversing the array, and, traversing from small to large to traverse, when the same number of two numbers appear, do not take any action, or the identification number stored in small numbers .
Here is my own proper code:
1 import java.util.Scanner; 2 public class Main { 3 4 public static void main(String[] args) { 5 Scanner sc = new Scanner(System.in); 6 int n = sc.nextInt(); 7 int s[]=new int[n+1]; 8 for(int i=1;i<=n;i++) 9 { 10 s[i]=sc.nextInt(); 11 } 12 int temp=0; 13 int mesure=1; 14 int max=s[1]; 15 int sum=0; 16 for(int i=1;i<=n;i++) 17 { 18 temp=s[i]; 19 for(int j=i+1;j<=n;j++) 20 { 21 if(s[j]==temp) 22 mesure++; 23 } 24 if(mesure>sum) 25 { 26 max=s[i]; 27 sum=mesure; 28 } 29 else if(mesure==sum) 30 { 31 if(max>s[i]) 32 max=s[i]; 33 } 34 mesure=1; 35 } 36 System.out.println(max); 37 } 38 }
My direct number input in order to exist in the array, and then double circulation, most frequently judged number appears, start with only 90 points, but later found the problem through regulation took 100 points.
I will continue to do simulation questions behind ccf, will write later in the blog.