CCF Computer professional Qualification Certification March 2015 The 2nd problem of digital sorting solution and thinking

Source: Internet
Author: User

Description of the problem given n integers, please count the number of occurrences of each integer, output in order of number of occurrences.

Input format the first line of input includes an integer n, which represents the number of a given number.
The second line consists of n integers, separated by a space between adjacent integers. Represents the given integer.

Output multiple lines in the output format. Each row consists of two integers representing a given integer and the number of times it appears.

Output in descending order of occurrences. Assuming that two integers occur as many times as possible, the output value is smaller and the output value is larger.

Example Input 12
5 2 3 3 1 3 4 2 5 2 3 5 Example output 3 4
2 3
5 3
1 1
4 1 Evaluation of Use cases scale and agreement

1≤n≤1000, the given number is a non-negative integer of not more than 1000.



Idea: This problem is not very difficult, the most difficult points in the same number of times to the lowest value of the first output, that is, the first word in descending order. The same number of times in ascending order of integers, the final output.

The beginning is to think of a treemap solution, and then sort by the V value, so when submitted, only 70 points. There's a case just. Think back or write another class. Save Num and Count, and then implement the comparator interface.

You can 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> ();        Add 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;//assume the same number of times, and then press the 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;}}



CCF Computer professional Qualification Certification March 2015 The 2nd problem of digital sorting solution and thinking

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.