PAT乙级1038

题目链接

https://pintia.cn/problem-sets/994805260223102976/problems/994805284092887040

题解一

这份代码最后一个点会超时

// PAT BasicLevel 1038
// https://pintia.cn/problem-sets/994805260223102976/problems/994805284092887040

#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    // n个学生及其分数
    int n;
    cin >> n;
    int *scores = new int[n];
    for(int i=0;i<n;++i){
        cin >> scores[i];
    }

    // k个查询的分数
    int k,search;
    cin >> k;
    while(k--){
        cin >> search;
        cout << count(scores, scores + n, search);
        if(k)
            cout << ' ';
    }
    
    // 释放内存
    delete[] scores;

    //system("pause");
    return 0;
}

题解二

这个所有点都过了。用数组存储各分数学生数量,下标是分数,数组元素值是数量。

// PAT BasicLevel 1038
// https://pintia.cn/problem-sets/994805260223102976/problems/994805284092887040

#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    // n个学生
    int n,score;
    cin >> n;

    // 各分数人数
    int count[101];
    fill(count,count+101,0);
    
    // 各分数的学生数量统计
    while(n--){
        cin >> score;
        count[score]++;
    }

    // k个查询的分数
    int k,search;
    cin >> k;
    while(k--){
        cin >> search;
        cout << count[search];
        if(k)
            cout << ' ';
    }

    //system("pause");
    return 0;
}

作者:@臭咸鱼

转载请注明出处:https://www.cnblogs.com/chouxianyu/

欢迎讨论和交流!


posted @ 2019-08-10 19:48  臭咸鱼  阅读(132)  评论(0编辑  收藏  举报