(2020最新網易筆試)人數統計

題目

小易的公司一共有n名員工, 第i個人每個月的薪酬是xi萬元。
現在小易的老闆向小易提了m次詢問, 每次詢問老闆都會給出一個整數k, 小易要快速回答老闆工資等於k的員工的數量。

輸入描述:

第一行,兩個空格間隔的整數m和n,表示人數和提問的次數
第二行,n個用空格間隔的整數xi,表示每名員工的薪酬
接下來有m行,每行一個整數,表示老闆的一次提問。
1<=m<=80000 ,1<=n<=100000 ,1<=xi<=500,000,000

​輸出描述:

m行,每行一個整數,表示對應提問的答案

示例1

輸入:
7 4
6 2 1 2 6 2 5
6
5
8
2
輸出:
2
1
0
3

步驟:

1.輸入兩個參數,m總人數,n提問次數
2.輸入一串數組爲員工薪酬,其中元素個數等於m
3.提問n次,每次輸入詢問的薪酬,輸出數組內相同薪酬的元素個數

import java.util.HashMap;
import java.util.Scanner;

public class countPeople {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();

        HashMap<Integer,Integer> map = new HashMap<>();
        for (int i = 0; i < n; i++){
            int t = in.nextInt();
            map.put(t,map.getOrDefault(t,0)+1);
        }
        for (int i = 0; i < m; i++){
            int r = in.nextInt();
            if (map.get(r) == null){
                System.out.println(0);
            }else
                System.out.println(map.get(r));
        }
    }
}



幾百本常用電子書免費領取:https://github.com/XiangLinPro/IT_book

在這裏插入圖片描述

If you ask for God to help you, it means you trust his ability.
If he doesn’t help you yet,
it means he trusts yours.

如果你向上天尋求幫助,
說明你相信老天的能力;
如果上天不幫你,則意味着他也相信你的能力。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章