PTA乙级题 1080. MOOC期终成绩 (25)

【题目链接】


最后一个点运行超时,询问大佬后得知,匹配名字处(即41行和55行处)过于复杂,第一个代码容易理解,但运行超时,第二个代码AC了。

#include<stdio.h>
#include<string.h>
typedef struct node
{
    char id[25];
    int a;
    int b;
    int c;
    int sum;
}Node;
int main()
{
    int x=0,score,i,j,q,w,e;
    char ID[25];
    scanf("%d %d %d",&q,&w,&e);
    getchar();
    Node s[q],temp;
    //建立数组 
    for (i=0;i<q;i++)
    {
        memset(ID,0,sizeof(ID));
        scanf("%s %d",ID,&score);
        if (score>=200)
        {
            strcpy(s[x].id,ID);
            s[x].a=score;
            x++;
        }
    }
    //设置默认值 
    for (i=0;i<x;i++)
    {
        s[i].b=-1;
        s[i].c=-1;
    }
    //读取期中成绩 
    for (i=0;i<w;i++)
    {
        memset(ID,0,sizeof(ID));
        scanf("%s %d",ID,&score);
        for (j=0;j<x;j++)
        {
            if (strcmp(ID,s[j].id)==0)
            {
                s[j].b=score;
                break;
            }
        }
    }
    //读取期末成绩 
    for (i=0;i<e;i++)
    {
        memset(ID,0,sizeof(ID));
        scanf("%s %d",ID,&score);
        for (j=0;j<x;j++)
        {
            if (strcmp(ID,s[j].id)==0)
            {
                s[j].c=score;
                break;
            }
        }
    }
    //计算总成绩
    for (i=0;i<x;i++)
    {
        if (s[i].b>s[i].c)
        s[i].sum=s[i].b*0.4+s[i].c*0.6+0.5;
        //此处应该+0.5,四舍五入。 
        else
        s[i].sum=s[i].c;
    }
    //排序
    for (i=0;i<x-1;i++)
    {
        for (j=0;j<x-1-i;j++)
        {
            if (s[j].sum<s[j+1].sum||(s[j].sum==s[j+1].sum&&strcmp(s[j].id,s[j+1].id)>0))
            {
                temp=s[j];
                s[j]=s[j+1];
                s[j+1]=temp;
            }
        }
    }
    for (i=0;i<x;i++)
    {
        if (s[i].sum>=60)
        printf("%s %d %d %d %d\n",s[i].id,s[i].a,s[i].b,s[i].c,s[i].sum);
    }
}

下面是芥末三文鱼提供的满分C++代码,有兴趣的同学可以研究一下。

#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <utility>
#include <array>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    size_t counts[3];
    map<string, array<int, 4>> m;
    cin >> counts[0] >> counts[1] >> counts[2];
    for (size_t i = 0; i < 3; i++)
        for (size_t j = 0; j < counts[i]; j++) {
            string name;
            array<int, 4> value = { -1, -1, -1 };
            cin >> name >> value[i];
            if (i == 0)
                m.emplace(make_pair(name, value));
            else {
                auto result = m.find(name);
                if (result != m.end())
                    result->second[i] = value[i];
                else
                    m.emplace(make_pair(name, value));
            }
        }
    vector<pair<string, array<int, 4>>> v(m.begin(), m.end());
    for (auto i = v.begin(); i != v.end(); i++) {
        if (i->second[1] > i->second[2]) {
            double middle = i->second[1] == -1 ? 0 : i->second[1];
            double final = i->second[2] == -1 ? 0 : i->second[2];
            double sum = (0.4 * middle) + (0.6 * final);
            i->second[3] = (int)sum + (sum - (int)sum > 0.5 ? 1 : 0);
        }
        else
            i->second[3] = i->second[2];
    }
    sort(v.begin(), v.end(), [](pair<string, array<int, 4>> a, pair<string, array<int, 4>> b) {
        if (a.second[3] != b.second[3])
            return isgreater(a.second[3], b.second[3]);
        else
            return a.first < b.first;
    });
    for (auto i : v) {
        if (i.second[3] >= 60 && i.second[0] >= 200) {
            cout << i.first;
            for (auto j : i.second)
                cout << " " << j;
            cout << endl;
        }
    }
    return 0;
}