题目:读入 N 名学生的成绩,将获得某一给定分数的学生人数输出。
输入格式:
第 1 行给出正整数 N,即学生总人数
随后一行给出 N 名学生的百分制整数成绩
最后一行给出要查询的分数个数 K,随后是 K 个分数,中间以空格分隔
输出格式:
在一行中按查询顺序给出得分等于指定分数的学生人数
思路:用map记录某一分数和该分数对应的人数。
代码:
#include<iostream>
#include<map>
using namespace std;
int main(){
int n;cin >> n;
map<int,int> mp; // 成绩 : 人数
for(int i = 0 ; i < n ; i ++){
int score;cin >> score;
if(mp.find(score) != mp.end())
mp[score] += 1;
else
mp.insert(make_pair(score,1));
}
int k;cin >> k;
for(int i = 0 ; i < k ; i ++){
int score;cin >> score;
if(mp.find(score) != mp.end())
cout << mp[score];
else
cout << "0";
if(i != k-1)
cout << " ";
else
cout << endl;
}
return 0;
}