#include <stdio.h>
#include <map>
using namespace std;
int main() {
int n; // 学生人数
scanf("%d",&n);
map<int,int> mp; // key为score,value为指定score的个数
for(int i = 0 ; i < n ; i++) {
int score; // 分数
scanf("%d",&score);
if(mp[score] != 0) {
mp[score] ++;
} else {
mp[score] = 1;
}
}
int num; // 查询的学生人数
scanf("%d",&num);
for(int i = 1 ; i <= num ; i++) {
int approvedScore; // 指定的分数
scanf("%d",&approvedScore);
if(mp[approvedScore]) {
printf("%d",mp[approvedScore]);
} else {
printf("%d",0);
}
if(i != num) {
printf(" ");
}
}
return 0;
}
java的通过不了PAT的最后一个测试点
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Map<Integer,Integer> map = new HashMap<>();
for(int i = 0 ; i < n ; i++) {
int score = sc.nextInt();
if(map.containsKey(score)) {
map.put(score, map.get(score) + 1);
} else {
map.put(score, 1);
}
}
int k = sc.nextInt();
for(int i = 0 ; i < k ; i++) {
int approvedScore = sc.nextInt();
if(i == k - 1) {
if(map.containsKey(approvedScore)) {
System.out.print(map.get(approvedScore));
} else {
System.out.print(0);
}
} else {
if(map.containsKey(approvedScore)) {
System.out.print(map.get(approvedScore) + " ");
} else {
System.out.print(0 + " ");
}
}
}
}
}