1. 程式人生 > >PAT乙級 解碼PAT准考證

PAT乙級 解碼PAT准考證

解碼PAT准考證 (25 分)

題目描述:

PAT 准考證號由 4 部分組成:

  • 第 1 位是級別,即 T 代表頂級;A 代表甲級;B 代表乙級;
  • 第 2~4 位是考場編號,範圍從 101 到 999;
  • 第 5~10 位是考試日期,格式為年、月、日順次各佔 2 位;
  • 最後 11~13 位是考生編號,範圍從 000 到 999。

現給定一系列考生的准考證號和他們的成績,請你按照要求輸出各種統計資訊。

輸入格式:

輸入首先在一行中給出兩個正整數 N(≤10​4​​)和 M(≤100),分別為考生人數和統計要求的個數。

接下來 N 行,每行給出一個考生的准考證號和其分數(在區間 [0,100] 內的整數),其間以空格分隔。

考生資訊之後,再給出 M 行,每行給出一個統計要求,格式為:型別 指令,其中

  • 型別 為 1 表示要求按分數非升序輸出某個指定級別的考生的成績,對應的 指令 則給出代表指定級別的字母;
  • 型別 為 2 表示要求將某指定考場的考生人數和總分統計輸出,對應的 指令 則給出指定考場的編號;
  • 型別 為 3 表示要求將某指定日期的考生人數分考場統計輸出,對應的 指令
     則給出指定日期,格式與准考證上日期相同。

輸出格式:

對每項統計要求,首先在一行中輸出 Case #: 要求,其中 # 是該項要求的編號,從 1 開始;要求 即複製輸入給出的要求。隨後輸出相應的統計結果:

  • 型別 為 1 的指令,輸出格式與輸入的考生資訊格式相同,即 准考證號 成績。對於分數並列的考生,按其准考證號的字典序遞增輸出(題目保證無重複准考證號);
  • 型別 為 2 的指令,按 人數 總分 的格式輸出;
  • 型別 為 3 的指令,輸出按人數非遞增順序,格式為 考場編號 總人數
    。若人數並列則按考場編號遞增順序輸出。

如果查詢結果為空,則輸出 NA

輸入樣例:

8 4
B123180908127 99
B102180908003 86
A112180318002 98
T107150310127 62
A107180908108 100
T123180908010 78
B112160918035 88
A107180908021 98
1 A
2 107
3 180908
2 999

輸出樣例:

Case 1: 1 A
A107180908108 100
A107180908021 98
A112180318002 98
Case 2: 2 107
3 260
Case 3: 3 180908
107 2
123 2
102 1
Case 4: 2 999
NA

解題思路: 

這題我在考試的時候寫了一個小時才寫出來,提交之後只有測試點0AC,測試點1、2WA,測試點3、4TLE。我知道WA和TLE的問題都是因為類別三排序和輸出有bug。類別三是要在考場人數降序的基礎上將考場編號升序輸出,我一開始用了map,結果不知道map怎麼先按value值大小降序,當value值相等時再按key值大小升序排序。於是我就用了個數組+雙重for迴圈來操作,果不其然TLE。然後我在考場就開始了長達半個小時的debug,結果該WA的還是WA,該TLE的還是TLE,時間還白白浪費掉了。今晚上我又花了一個小時來不停地debug,終於發現造成TLE的原因:①cout和stdout的同步,導致超時;②比較函式傳遞引數的時候引用傳參要比較快。下面三張截圖比較一下19分 22分 25分的執行時間。

19分,後倆個測試直接TLE。

22分,測試點4耗時195msAC,測試點3依舊TLE。

25分,測試點4耗時從195ms邊成了69ms,測試點3耗時147msAC。

15分程式碼:

測試點0AC,測試點1、2WA,測試點3、4TLE。

#include <bits/stdc++.h>
using namespace std;

struct stu
{
    string ID;   //准考證號
    int score;   //分數
};

bool Cmp(stu a,stu b)
{
    //lambda表示式,先按分數降序排列,若分數相等則按准考證號升序排列
    return a.score!=b.score? a.score>b.score : a.ID<b.ID;
}

int main()
{
    int N,M;
    ios::sync_with_stdio(false);   //取消cin和stdin的同步
    cin >> N >> M;
    vector<stu> v(N);
    for (int i = 0; i < N; i++)
    {
        cin >> v[i].ID >> v[i].score;
    }
    int xh = 0;  //序號
    while(M--)
    {
        xh++;
        bool flag = false;   //false時輸出NA
        int lx;     //型別
        cin >> lx;
        if(lx == 1)
        //型別1要求按分數非升序輸出某個指定級別的考生的成績
        {
            char zl;
            cin >> zl;
            cout << "Case " << xh << ": " << lx << " " << zl << endl;
            sort(v.begin(),v.end(),Cmp);
            for (auto it = v.begin(); it != v.end(); it++)
            {
                if(it->ID[0] == zl)
                {
                    flag = true;
                    cout << it->ID << " " << it->score << endl;
                }
            }
        }
        else if(lx == 2)
        //型別2要求將某指定考場的考生人數和總分統計輸出
        {
            int zl;
            cin >> zl;
            cout << "Case " << xh << ": " << lx << " " << zl << endl;
            int count_r = 0,count_f=0;  //人 分
            for (auto it = v.begin(); it != v.end(); it++)
            {
                string tempstr = "";
                for (int i = 1; i <= 3; i++)
                {
                    tempstr += it->ID[i];
                }
                int temp = atoi(tempstr.c_str());
                if(temp == zl)
                {
                    count_r++;
                    count_f += it->score;
                    flag = true;
                }
            }
            if(!flag)    //查詢結果為空
            {
                cout << "NA" << endl;
            }
            else
            {
                cout << count_r << " " << count_f << endl;
            }
        }
        else if(lx == 3)
        //型別3要求將某指定考場的考生人數和總分統計輸出
        {
            string zl;
            cin >> zl;
            cout << "Case " << xh << ": " << lx << " " << zl << endl;
            int a[1000];
            memset(a,0,sizeof(a));
            for (auto it = v.begin(); it != v.end(); it++)
            {
                string tempdata = "";  //日期
                for (int i = 4;i <= 9; i++)
                {
                    tempdata += it->ID[i];
                }
                if(tempdata == zl)
                {
                    flag = true;
                    string tempstr = "";  //考場
                    for(int i=1;i<=3;i++)
                    {
                        tempstr += it->ID[i];
                    }
                    int temp = atoi(tempstr.c_str());
                    a[temp]++;
                }
            }
            if(!flag)
            {
                cout << "NA" << endl;
            }
            else
            {
                for (int i = 100; i > 0; i--)
                {
                    for (int j = 100; j < 1000; j++)
                    {
                        if(a[j] == i)
                        cout << j << " " << a[j] << endl;
                    }
                }
            }

        }
    }
    return 0;
}

19分程式碼:

測試點3、4依舊TLE。跟15分的程式碼區別:①在類別1中加入了一個if(!flag)輸出NA的語句;②把類別3的陣列換成了vector+map。

#include <bits/stdc++.h>
using namespace std;

struct stu   //考生
{
    string ID;   //准考證號
    int score;   //分數
};

struct room   //考場
{
    string ID;   //考場編號
    int count;    //考場人數
};

bool Cmp1(stu a,stu b)   //型別1用到的比較方法
{
    //lambda表示式,先按分數降序排列,若分數相等則按准考證號升序排列
    return a.score!=b.score? a.score>b.score : a.ID<b.ID;
}

bool Cmp3(room a,room b)   //型別3用到的比較方法
{
    //lambda表示式,先按考場人數降序排列,若考場人數相同則按考場標號升序排列
    return a.count!=b.count? a.count>b.count : a.ID<b.ID;
}

int main()
{
    int N,M;
    ios::sync_with_stdio(false);   //取消cin和stdin的同步
    cin >> N >> M;
    vector<stu> v(N);
    for (int i = 0; i < N; i++)
    {
        cin >> v[i].ID >> v[i].score;
    }
    int xh = 0;  //序號
    while(M--)
    {
        xh++;
        bool flag = false;   //false時輸出NA
        int lx;     //型別
        cin >> lx;
        if(lx == 1)
        //型別1要求按分數非升序輸出某個指定級別的考生的成績
        {
            char zl;
            cin >> zl;
            cout << "Case " << xh << ": " << lx << " " << zl << endl;
            sort(v.begin(),v.end(),Cmp1);
            for (auto it = v.begin(); it != v.end(); it++)
            {
                if(it->ID[0] == zl)
                {
                    flag = true;
                    cout << it->ID << " " << it->score << endl;
                }
            }
            if(!flag)
            {
                cout << "NA" << endl;
            }
        }
        else if(lx == 2)
        //型別2要求將某指定考場的考生人數和總分統計輸出
        {
            int zl;
            cin >> zl;
            cout << "Case " << xh << ": " << lx << " " << zl << endl;
            int count_r = 0,count_f=0;  //人 分
            for (auto it = v.begin(); it != v.end(); it++)
            {
                string tempstr = it->ID.substr(1,3);
                int temp = atoi(tempstr.c_str());
                if(temp == zl)
                {
                    count_r++;
                    count_f += it->score;
                    flag = true;
                }
            }
            if(!flag)    //查詢結果為空
            {
                cout << "NA" << endl;
            }
            else
            {
                cout << count_r << " " << count_f << endl;
            }
        }
        else if(lx == 3)
        //型別3要求將某指定考場的考生人數和總分統計輸出
        {
            string zl;
            cin >> zl;
            cout << "Case " << xh << ": " << lx << " " << zl << endl;
            vector<room> kc;
            map<string,int> m;
            for (auto it = v.begin(); it != v.end(); it++)
            {
                if(it->ID.substr(4,6) == zl)
                {
                    flag = true;
                    string tempstr = it->ID.substr(1,3);  //考場
                    //int temp = atoi(tempstr.c_str());
                    m[tempstr]++;
                }
            }
            for (auto it : m)   //把map全部推入vector中來排序
            {
                kc.push_back({it.first,it.second});
            }
            sort(kc.begin(),kc.end(),Cmp3);
            if(!flag)
            {
                cout << "NA" << endl;
            }
            else
            {
                for (int i = 0; i < kc.size(); i++)
                {
                    cout << kc[i].ID  << " " << kc[i].count << endl;
                }
            }
        }
    }
    return 0;
}

22分程式碼:

測試點3TLE。跟19分程式碼的區別:看了大佬的程式碼,把排序函式的傳引數改成了引用傳參,她說這樣更快。但是依舊有測試用例TLE。

#include <bits/stdc++.h>
using namespace std;
 
struct stu   //考生
{
    string ID;   //准考證號
    int score;   //分數
};
 
struct room   //考場
{
    string ID;   //考場編號
    int count;    //考場人數
};
 
bool Cmp1(stu &a,stu &b)   //型別1用到的比較方法
{
    //lambda表示式,先按分數降序排列,若分數相等則按准考證號升序排列
    return a.score!=b.score? a.score>b.score : a.ID<b.ID;
}
 
bool Cmp3(room &a,room &b)   //型別3用到的比較方法
{
    //lambda表示式,先按考場人數降序排列,若考場人數相同則按考場標號升序排列
    return a.count!=b.count? a.count>b.count : a.ID<b.ID;
}
 
int main()
{
    int N,M;
    ios::sync_with_stdio(false);   //取消cin和stdin的同步
    cin >> N >> M;
    vector<stu> v(N);
    for (int i = 0; i < N; i++)
    {
        cin >> v[i].ID >> v[i].score;
    }
    int xh = 0;  //序號
    while(M--)
    {
        xh++;
        bool flag = false;   //false時輸出NA
        int lx;     //型別
        cin >> lx;
        if(lx == 1)
        //型別1要求按分數非升序輸出某個指定級別的考生的成績
        {
            char zl;
            cin >> zl;
            cout << "Case " << xh << ": " << lx << " " << zl << endl;
            sort(v.begin(),v.end(),Cmp1);
            for (auto it = v.begin(); it != v.end(); it++)
            {
                if(it->ID[0] == zl)
                {
                    flag = true;
                    cout << it->ID << " " << it->score << endl;
                }
            }
            if(!flag)
            {
                cout << "NA" << endl;
            }
        }
        else if(lx == 2)
        //型別2要求將某指定考場的考生人數和總分統計輸出
        {
            int zl;
            cin >> zl;
            cout << "Case " << xh << ": " << lx << " " << zl << endl;
            int count_r = 0,count_f=0;  //人 分
            for (auto it = v.begin(); it != v.end(); it++)
            {
                string tempstr = it->ID.substr(1,3);
                int temp = atoi(tempstr.c_str());
                if(temp == zl)
                {
                    count_r++;
                    count_f += it->score;
                    flag = true;
                }
            }
            if(!flag)    //查詢結果為空
            {
                cout << "NA" << endl;
            }
            else
            {
                cout << count_r << " " << count_f << endl;
            }
        }
        else if(lx == 3)
        //型別3要求將某指定考場的考生人數和總分統計輸出
        {
            string zl;
            cin >> zl;
            cout << "Case " << xh << ": " << lx << " " << zl << endl;
            vector<room> kc;
            map<string,int> m;
            for (auto it = v.begin(); it != v.end(); it++)
            {
                if(it->ID.substr(4,6) == zl)
                {
                    flag = true;
                    string tempstr = it->ID.substr(1,3);  //考場
                    //int temp = atoi(tempstr.c_str());
                    m[tempstr]++;
                }
            }
            for (auto it : m)   //把map全部推入vector中來排序
            {
                kc.push_back({it.first,it.second});
            }
            sort(kc.begin(),kc.end(),Cmp3);
            if(!flag)
            {
                cout << "NA" << endl;
            }
            else
            {
                for (int i = 0; i < kc.size(); i++)
                {
                    cout << kc[i].ID  << " " << kc[i].count << endl;
                }
            }
        }
    }
    return 0;
}

25分程式碼:

跟22分程式碼的區別:①把所有的cout語句換成了printf,因為cout和stdout保持同步導致速度很慢,又沒有類似cin和stdin的取消同步語句ios::sync_with_stdio(false)。需要注意的是printf裡的%s是不能直接輸出string型的,必須先用c_str()轉換成*char型。

#include <bits/stdc++.h>
using namespace std;

struct stu   //考生
{
    string ID;   //准考證號
    int score;   //分數
};

struct room   //考場
{
    string ID;   //考場編號
    int count;    //考場人數
};

bool Cmp1(stu &a,stu &b)   //型別1用到的比較方法
{
    //lambda表示式,先按分數降序排列,若分數相等則按准考證號升序排列
    return a.score!=b.score? a.score>b.score : a.ID<b.ID;
}

bool Cmp3(room &a,room &b)   //型別3用到的比較方法
{
    //lambda表示式,先按考場人數降序排列,若考場人數相同則按考場標號升序排列
    return a.count!=b.count? a.count>b.count : a.ID<b.ID;
}

int main()
{
    int N,M;
    ios::sync_with_stdio(false);   //取消cin和stdin的同步
    cin >> N >> M;
    vector<stu> v(N);
    for (int i = 0; i < N; i++)
    {
        cin >> v[i].ID >> v[i].score;
    }
    int xh = 0;  //序號
    while(M--)
    {
        xh++;
        bool flag = false;   //false時輸出NA
        int lx;     //型別
        cin >> lx;
        if(lx == 1)
        //型別1要求按分數非升序輸出某個指定級別的考生的成績
        {
            char zl;
            cin >> zl;
            printf("Case %d: %d %c\n", xh, lx, zl);
            sort(v.begin(),v.end(),Cmp1);
            for (auto it = v.begin(); it != v.end(); it++)
            {
                if(it->ID[0] == zl)
                {
                    flag = true;
                    printf("%s %d\n", it->ID.c_str(), it->score);
                }
            }
            if(!flag)
            {
                printf("NA\n");
            }
        }
        else if(lx == 2)
        //型別2要求將某指定考場的考生人數和總分統計輸出
        {
            int zl;
            cin >> zl;
            printf("Case %d: %d %d\n", xh, lx, zl);
            int count_r = 0,count_f=0;  //人 分
            for (auto it = v.begin(); it != v.end(); it++)
            {
                string tempstr = it->ID.substr(1,3);
                int temp = atoi(tempstr.c_str());
                if(temp == zl)
                {
                    count_r++;
                    count_f += it->score;
                    flag = true;
                }
            }
            if(!flag)    //查詢結果為空
            {
                printf("NA\n");
            }
            else
            {
                printf("%d %d\n", count_r, count_f);
            }
        }
        else if(lx == 3)
        //型別3要求將某指定考場的考生人數和總分統計輸出
        {
            string zl;
            cin >> zl;
            printf("Case %d: %d %s\n", xh, lx, zl.c_str());
            vector<room> kc;
            unordered_map<string,int> m;
            for (auto it = v.begin(); it != v.end(); it++)
            {
                if(it->ID.substr(4,6) == zl)
                {
                    flag = true;
                    string tempstr = it->ID.substr(1,3);  //考場
                    //int temp = atoi(tempstr.c_str());
                    m[tempstr]++;
                }
            }
            for (auto it : m)   //把map全部推入vector中來排序
            {
                kc.push_back({it.first,it.second});
            }
            sort(kc.begin(),kc.end(),Cmp3);
            if(!flag)
            {
                printf("NA\n");
            }
            else
            {
                for (int i = 0; i < kc.size(); i++)
                {
                    printf("%s %d\n", kc[i].ID.c_str(), kc[i].count);
                }
            }
        }
    }
    return 0;
}