思路,创一个数组,输入的数相等于数组的下标,每输入一个数,就对应的下标+1;然后遍历,找出数组里的最大值,输出该下标和值,然后置0,当最大值的为0时,终止循环。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] count = new int[1001];
int n = input.nextInt();
int a=-1;
for (int i = 0; i < n;i++){
a = input.nextInt();
count[a]++;
}
for (int i = 0;i < n;i++){
int max = 0;
for (int j = 1; j < 1001;j++){
if (count[j] > count[max]){
max = j;
}
}
if (count[max]==0){
break;
}
System.out.printf("%d %d\n",max,count[max]);
count[max]=0;
}
}
}
原题链接:http://118.190.20.162/view.page?gpid=T26