题解 | #统计同成绩学生(20)#

统计同成绩学生(20)

http://www.nowcoder.com/questionTerminal/3df4810cc0664b8bb848d785f68f7c9b

#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 + " ");
                }
            }
        }
    }
}
全部评论

相关推荐

04-29 22:35
门头沟学院 Java
牛友说改了名字能收到offer:旧图新发查看图片
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-23 16:31
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务