Description: given n integers, count the number of occurrences of each integer and output in the order of occurrences, from many to fewer.
Input: The first line of input contains an integer n, which indicates the number of a given number.
The second row contains n integers, separated by a space between adjacent integers, representing the given integer.
Output: Outputs 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.
Input
125 2 3 3 1 3 4 2 5 2 3 5
Output
3 42 35 31 14 1
Analysis: The difficulty lies in the number of occurrences in descending order of the output of integers and the number of times he appears, so need a two-dimensional array a[10][2], where a[][0] stored integer, a[][1] The number of occurrences of the integer, sorting the whole exchange, if the same line output smaller output larger, Here is a little trick to look at the code in detail.
1#include <iostream>2 using namespacestd;3 4 voidSortinta[][2])5 {6 intmin;7 intn =0;8 for(inti =0; I <Ten; i++)//Only the number is not 0 of the order, because the initial is from small to large, the sort is completed after the same as from small to large9 {Ten if(a[i][1] !=0) Onen++; A } - for(inti =0; I < n; i++) - { the for(inti =0;; i++) - { - if(a[i][1] !=0) - { +Min =i; - Break; + } A } at - for(intj =0; J <=9I J + +) - { - if(a[j][1]!=0) - if(a[min][1] >= a[j][1]) -Min =J; in } - intx = a[min][0], y = a[min][1]; toa[min][0] = a[9-i][0]; a[min][1] = a[9-i][1]; +a[9-i][0] = x; a[9-i][1] =y; - } the } * intMain () $ {Panax Notoginseng intn,x; - Static inta[Ten][2]; theCIN >>N; + for(inti =0; I <Ten; i++) Aa[i][0] =i; the for(inti =0; I < n; i++) + { -CIN >>x; $a[x][1]++; $ } - sort (a); - for(inti =0; I <Ten; i++) the { - if(a[i][1] !=0)Wuyicout << a[i][0] <<" "<< a[i][1] <<Endl; the } -System"Pause"); Wu return 0; -}
PS: Feel write trouble, welcome to have a simplified method of communication ....
Digital sorting issues (C + + implementation)