Problem description given n integers, count the number of occurrences of each integer and output it in the order of occurrence, from the number of occurrences to the least. Input format the first line of input contains an integer n, which represents the number of a given number.
The second row contains n integers, separated by a space between adjacent integers, representing the given integer. Output formats output multiple lines, each containing two integers, representing a given integer and the number of times it appears. Output in descending order of occurrences. If two integers occur as many times as possible, the output value is smaller and the output value is larger. Sample Input 12
5 2 3 3 1 3 4 2 5 2 3 5 sample output 3 4
2 3
5 3
1 1
4 1 Evaluation use case scale and conventions
1≤n≤1000, the given number is a non-negative integer of not more than 1000.
Idea: This problem is not difficult, the most difficult points in the same number of times to the lowest value of the first output, that is, first by the number of words in descending order, and then in the same order in ascending order of integers, the final output.
The first is to think of a treemap solution, and then by the V-value sort, so when submitted, only 70 points, there is case. Think back or write another class. Save Num and Count, and then implement the comparator interface. Sort by rules.
The code may not be very streamlined, and if there is a better way, you can thread the description.
The code is as follows:
Package Sds;import java.util.*;p ublic class Main {public static void main (string[] args) {//Accept input Scanner sc = new Scanner (Sy stem.in); int n = sc.nextint ();//get array aint[] A = new Int[n];for (int i = 0; i < n; i++) {A[i] = Sc.nextint ();} Number of statistics map<integer,integer> map = new Hashmap<integer, integer> (); for (int i = 0; i < n; i++) {if (Map.get (a[i) ) = = null) {map.put (a[i],1);} Else{map.put (A[i],map.get (a[i]) + 1); }} valuecomparator BVC = new Valuecomparator (); list<numcount> list = new arraylist<numcount> (); Added to list for (int key:map.keySet ()) {Numcount count = new Numcount (key, Map.get (key)); List.add (count); } collections.sort (list, BVC);//Sort//print for (int i = 0; i < list.size (); i++) {System.out.pri Ntln (List.get (i). Getnum () + "" + list.get (i). GetCount ()); }}}//implementation Sort Class Valuecomparator implements comparator<numcount> {@Overridepublic int compare (Numcount O1, Numcount O2) {iF (O1.getcount () > O2.getcount ()) return-1;//descending order if (O1.getcount () < O2.getcount ()) Return 1;if (O1.getnum () > O2.getnum ()) return 1;//if the number of times is the same, then by integer Ascending if (O1.getnum () < O2.getnum ()) Return-1;return 0;} }//Save numbers and times class numcount{private int num;private int count;public numcount (int num,int count) {this.num = Num;this.count = Count;} public int Getnum () {return num;} public void setnum (int num) {this.num = num;} public int GetCount () {return count;} public void SetCount (int count) {This.count = count;}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
CCF Computer professional Qualification Certification March 2015 The 2nd problem of digital sorting solution and thinking