• Java实现 蓝桥杯VIP 算法训练 数的统计


    问题描述
    在一个有限的正整数序列中,有些数会多次重复出现在这个序列中。
    如序列:3,1,2,1,5,1,2。其中1就出现3次,2出现2次,3出现1 次,5出现1次。
    你的任务是对于给定的正整数序列,从小到大依次输出序列中出现的数及出现的次数。
    输入格式
    第一行正整数n,表示给定序列中正整数的个数。
    第二行是n 个用空格隔开的正整数x,代表给定的序列。
    输出格式
    若干行,每行两个用一个空格隔开的数,第一个是数列中出现的数,第二个是该数在序列中出现的次数。
    样例输入
    12
    8 2 8 2 2 11 1 1 8 1 13 13
    样例输出
    1 3
    2 3
    8 3
    11 1
    13 2
    数据规模和约定
    数据:n<=1000;0<x<=1000,000。

    import java.util.Scanner;
    import java.util.SortedMap;
    import java.util.TreeMap;
    
    
    public class 数的统计 {
    
    	 
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            int n = in.nextInt();
            SortedMap<Integer, Integer> hash = new TreeMap<Integer, Integer>();
            for (int i = 0; i < n; i++) {
                int x = in.nextInt();
                if (!hash.containsKey(x)) {
                    hash.put(x, 1);
                } else {
                    int cnt = hash.get(x) + 1;
                    hash.put(x, cnt);
                }
            }
            in.close();
     
            for (Integer key : hash.keySet()) {
                System.out.println(key + " " + hash.get(key));
            }
        }
    
    
    }
    
    
  • 相关阅读:
    python day1
    Vue与react的择决
    CommonJS规范
    js面向对象的程序设计
    正则匹配所有的a标签
    js-静态、原型、实例属性
    js中参数不对应问题
    sublime常用快捷键
    JSON.parse()和JSON.stringify()
    setAttribute()
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13079286.html
Copyright © 2020-2023  润新知