1

【问题描述】

输入一组无序的整数,编程输出其中出现次数最多的整数及其出现次数。

【输入形式】

先从标准输入读入整数的个数(大于等于1,小于等于100),然后在下一行输入这些整数,各整数之间以一个空格分隔。

【输出形式】

在标准输出上输出出现次数最多的整数及其出现次数,两者以一个空格分隔;若出现次数最多的整数有多个,则输出位置靠前的数。

【样例输入】

10

0 -50 0 632 5813 -50 9 -50 0 632

【样例输出】

0 3

【样例说明】

输入了10个整数,其中出现次数最多的是0和-50,都是出现3次,但0靠前,所以输出0 3。

【评分标准】该程序要求输出出现次数最多的整数和出现次数。
`
import java.util.*;

public class Main {
static Map<Integer,Integer> m=new HashMap<>();
static int max;
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
max=sc.nextInt();
count(max);
for(int i=1;i<n;i++){
int x=sc.nextInt();
count(x);
if(m.get(x)>m.get(max)){
max=x;
}
}
System.out.println(max+" "+m.get(max));
}
public static void count(int x){
if(m.containsKey(x)){
m.put(x,m.get(x)+1);
}else{m.put(x,1);}
}
}
`
样例输出为-50 3,与预期不符。

posted @ 2021-10-06 17:15  晴影  阅读(191)  评论(0)    收藏  举报