JAVA statistics of the number of training algorithms Blue Bridge

Description of the problem
  in a finite sequence of positive integers, some number will be repeated many times in this sequence.
  The sequence: 3,1,2,1,5,1,2. Wherein 1 appeared three times, 2 occurs twice, 3 1 occurrence, 5 appear more than once.
  Your task is, for a given sequence of positive integers, from small to large order of output sequence number and the number of occurrences that occur.
Input format
  of the first row positive integer n, the number of a given sequence of CKS integers.
  The second line is a space-separated n a positive integer x, representative of a given sequence.
Output Format
  number of lines, each line separated by a space of two numbers, the first number is a number appearing in the column, the second number is the number that appears in the sequence.
Sample input
12 is
828,221,111,811,313
sample output
. 1. 3
2. 3
. 8. 3
. 11. 1
13 is 2
knowledge: the interface is similar to SortedSet, SortedMap is a structure, the Map to be sorted, which is a comparison common implementation class is TreeMap.
The TreeMap put (K key, V value) at each method to add an element, automatically sorted.

natural order TreeMap () Constructs a new key using the empty tree map. dendrogram (Comparator <?) (super K> comparator) Constructs a new, empty tree map, according to a given comparison It is sorted. Dendrogram (Map <?) Extension K? Extended V> m) Constructs a new map given tree map having a relationship with the same mapping to the mappings natural ordering of its keys.
Details Beacon: https: //www.itzhai.com/treemap-sortedmap-interface-implementation-class-introduction-and-implementation-of-custom-comparator-comparator.html

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		SortedMap<Integer, Integer> hash = new TreeMap<Integer, Integer>();
		for (int i = 0; i < n; i++) {
			int x = scanner.nextInt();
			if (!hash.containsKey(x)) {
				hash.put(x, 1);
			} else {
				int cnt = hash.get(x) + 1;
				hash.put(x, cnt);
			}
		}
		scanner.close();
		for (Integer key : hash.keySet()) {
			System.out.println(key+" "+hash.get(key));
		}
	}

Small Theater: cheerful, full of hope.

Published 116 original articles · won praise 113 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_43771695/article/details/104714779