AcWing 3213. 数字排序java
原题链接
简单
作者:
huaqingren
,
2021-02-26 14:40:24
,
所有人可见
,
阅读 404
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Main
{
private static int N=1010;
private static int[] a=new int[N];
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
while(n--!=0)
{
int x=scan.nextInt();
a[x]++;
}
List<Node> list=new ArrayList<>();
for(int i=0;i<=1000;i++)
{
if(a[i]>0)
{
list.add(new Node(i,a[i]));
}
}
Collections.sort(list);
for(int i=0;i<list.size();i++)
System.out.println(list.get(i).num+" "+list.get(i).cnt);
scan.close();
}
private static class Node implements Comparable<Node>
{
private int num;
private int cnt;
public Node(int num,int cnt)
{
this.num=num;
this.cnt=cnt;
}
@Override
public int compareTo(Node o)
{
if(this.cnt-o.cnt!=0)
return o.cnt-this.cnt;
else
return this.num-o.num;
}
}
}