由CSP 题目引发的关于Map的一些想法

本文通过一道CSP题目,讨论如何利用Map统计一组正整数中出现次数最多的数。当有多个数出现次数相同时,应输出其中最小的一个。文章提供了Java代码实现,展示了Map在处理此类问题时的作用,强调了Map中key的唯一性特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CSP题目如下:

问题描述

给定n个正整数,找出它们中出现次数最多的数。如果这样的数有多个,请输出其中最小的一个。

输入格式

输入的第一行只有一个正整数n(1 ≤ n ≤ 1000),表示数字的个数。

输入的第二行有n个整数s1, s2, …, sn (1 ≤ si ≤ 10000, 1 ≤ i ≤ n)。相邻的数用空格分隔。

输出格式

输出这n个次数中出现次数最多的数。如果这样的数有多个,输出其中最小的一个。

样例输入

6

10 1 10 20 30 20

样例输出

10

----------------------------------------------------------------------------------------------------------------

现在开始作答:

标准答案:

直接附上代码,如下

import java.util.*;
public class Test{
	public static void main(String[] args){
		new Test().run(); 
	}
	public void run(){
		Scanner sc=new Scanner(System.in);
		int n;
		n=sc.nextInt();
		int[] count=new int[10001];
		for(int i=0;i<n;i++){
			++count[sc.nextInt()];
	    }
	int maxCount=-1;
	int result=0;
	for(int i=1;i<=10000;i++){
		if(count[i]>maxCount){
			maxCount=count[i];
			result=i;
		}
	}
	System.out.println(result);
	}
}

引发思考1(使用map,得到数组中出现次数最多的数或者字符)
所有Map操作中key的值是不能重复的,例如,HashMap操作时key是不能重复的,如果重复则肯定会覆盖之前的内容.

代码如下:

import java.util.*;


public class mapTest{
	public static void main(String[] args) {
		String [] arry = {"h","h","h","h","h","h","2","2","2","2","3","3","3","3"};
		Map<String,Integer> map = new HashMap<String, Integer>();  //声明一个Map
		for(int i =0;i<arry.length;i++){
			if(map.containsKey(arry[i])){
				map.put(arry[i], map.get(arry[i])+1);
			//System.out.print(map.get(arry[i]));测试结果
			//if(null!= map.get(arry[i])){ //get()从map中取值
			//map.put(arry[i], map.get(arry[i-1])+1); //value+1  put向map中放值
			//System.out.print(map.get(arry[i-1]));
			}else{
				map.put(arry[i],1);//字符第一次出现时,计数为1
			}
		}
		Iterator it = map.entrySet().iterator();  //map接口转为set接口、迭代器使用,返回一个Iterator单向遍历,实例化Iterator  
		String result="";
		int maxCount=-1;
		while(it.hasNext()){//检查序列中是否还有元素 
			Map.Entry entry = (Map.Entry) it.next();   //每个对象都是Map.Entry实例    
			String  key  =  entry.getKey().toString();   //得到键   
			int  value  =  Integer.parseInt(entry.getValue().toString()); //得到值
			//System.out.println("key is :"+key+"---value :"+value);//key存数组元素,value存数组元素出现次数
		    if(value>maxCount){
		    	maxCount=value;
		    	result=key;
		    	//System.out.println("key is :"+key+"---value :"+value);
		    }
		} 
		System.out.println(result);
	}
}

引发思考2

(使用map,统计数组重复数字出现的次数)

import java.util.*;
public class Test1{
	public static void main(String[] args){
	   int num[]= {6,6,6,3,6,3,2,2,3,1};
	   MyArr arr=new MyArr();
	   for(int i=0;i<num.length;i++){
	   	  arr.setNum(num[i]);
	   }
	   arr.getNum();
	}
}
class MyArr{
	Map<Integer,Integer> map=new HashMap<Integer,Integer>();
	public void setNum(int num){
		if(map.containsKey(num)){  //判断Map集合对象中是否包含指定的键名
			map.put(num,map.get(num)+1);
		}
		else{
			map.put(num,1);
		}
	}
	public void getNum(){
		for(Integer key:map.keySet()){ //获取map集合的所有键名
			System.out.println(key+"出现"+map.get(key)+"次");
		}
	}
}

引发思考3(不适用map)

附上代码,如下

public class Test2 {
	public static void main(String[] args){
		int array[]={5, 10, 10, 5, 2, 5, 3, 5, 10, 5, 2, 5, 5, 10, 1, 5, 1};
		Arrays.sort(array);// 给数组排序
		int count=0;
		int temp=array[0];
		for(int i=0;i<array.length;i++){
			if(temp !=array[i]){
				System.out.println(temp+"出现:"+count+"次");  //输出上一轮的结果
				temp = array[i];
				count=1;
		}else{
			count++;
		}
		if(i==array.length-1){
			System.out.println(temp+"出现:"+count+"次");
		}
		}
	}
}


分享知识、分享快乐。明天,再接再厉。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值