Question number: | 201503-2 |
Question name: | Number sort |
time limit: | 1.0s |
Memory limit: | 256.0MB |
Problem Description: | Problem Description Given n integers, please count the number of occurrences of each integer, and output them in order of the number of occurrences. Input format The first line of input contains an integer n , which represents the number of given numbers. Output format Output multiple lines, each line contains two integers, representing a given integer and the number of times it appears. Output in descending order of occurrence. If two integers appear the same number of times, the smaller value is output first, and then the larger value is output. Sample input 12 Sample output 3 4 Evaluation use case scale and conventions 1 ≤ n ≤ 1000, the numbers given are all non-negative integers not exceeding 1000. |
import java.util.Scanner;
public class 数字排序201503_2 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] a=new int[1001];
for(int i=1;i<=n;i++){
a[sc.nextInt()]+=1;
}
int[] index=new int[n+1];
int[] max=new int[n+1];
for(int i=1;i<=n;i++){
max[i]=0;
for(int j=0;j<a.length;j++){
if(a[j]>0){
if(max[i]<a[j]){
max[i]=a[j];
index[i]=j;
}
}
}
a[index[i]]=0;
if(max[i]!=0){
System.out.println(index[i]+" "+max[i]);
}
}
}
}