記第一次參加PAT(附18.12.09PAT乙級題解)


寫在前面:

18年12月09日 第一次參加PAT乙級,在停止提交的那一刻,我的心里只有倆個字:遺憾。這次乙級的后倆題是甲級的前倆題一模一樣,不過甲級題是英文。並不是輸在了題目有多難,以我這個菜雞的水平,這套乙級題應該至少能考85+的。可是由於我時間分配的不合理,導致了這次考試的失敗。真是芝麻沒撿到還弄丟了個大西瓜。考試時間總共是3個小時,前倆題全部AC我用了40分鍾,還有2個小時10分鍾才結束考試,然后心里就有點飄啊。看完第三題之后,我小聲嗶嗶了一句:“這個題目也太水了吧。”然后我直接跳到了第4題寫,因為第3題真的太水了。第四題20分得了18分用時30分鍾,寫完后還有1個小時40分鍾結束考試。最后一題25分!寫了我一個多小時提交代碼之后只得了15分,然后我debug了半個多小時,最后10分鍾才猛然發現第3道水題還沒寫,然后我就很慌張。其實考完后發現第三題就直接A+B,輸出的時候用map來記錄字符不要輸出重復字符就行了。可是只剩最后10分鍾我這個菜雞慌了,在考場發慌的時候思路是凌亂的最后這道題是空白的,沒有提交過一次。總結一下教訓:以后考試不要飄!不要膨脹!不要跳過水題先寫難題!不要慌!先把所有題都提交得分后再debug!

涉及到的一些知識:

①map的用法;②string型轉char*型函數:c_str();③char*型轉int型函數:atoi();④vector的用法。

題目列表: 

第5題萬惡的TLE,我考完之后都debug了3個版本才全AC。這些題都可以點擊跳轉。

1. N-自守數 (15 分)

2. 最好吃的月餅 (20 分)

3. 字符串A+B (20 分)

4. 谷歌的招聘 (20 分)

5. 解碼PAT准考證 (25 分)

題目解析:

1. N-自守數 (15 分)

題目描述:

如果某個數 K 的平方乘以 N 以后,結果的末尾幾位數等於 K,那么就稱這個數為“N-自守數”。例如 3×92​2​​=25392,而 25392 的末尾兩位正好是 92,所以 92 是一個 3-自守數。

本題就請你編寫程序判斷一個給定的數字是否關於某個 N 是 N-自守數。

輸入格式:

輸入在第一行中給出正整數 M(≤20),隨后一行給出 M 個待檢測的、不超過 1000 的正整數。

輸出格式:

對每個需要檢測的數字,如果它是 N-自守數就在一行中輸出最小的 N 和 NK​2​​ 的值,以一個空格隔開;否則輸出 No。注意題目保證 N<10。

輸入樣例:

3
92 5 233

輸出樣例:

3 25392
1 25
No

解題思路:

這題我自定義了一個fun函數來計算該函數的形參n向上取整的len是10、100還是1000。用for循環在1~10中尋找那個能使(K*K*N)%len = K成立的N,要是找到了就用ans來記錄這個N,跳出循環輸出ans。要是for循環結束還沒找到,就輸出No。

AC代碼:

 1 #include <bits/stdc++.h>
 2 using namespace std;  3 
 4 int fun(int n)  5 {  6     int count = 0;  7     while(n > 0)  8  {  9         count++; 10         n /= 10; 11  } 12     int sum = 1; 13     for (int i = 0; i < count; i++) 14  { 15         sum *= 10; 16  } 17     return sum; 18 } 19 
20 int main() 21 { 22     int M; 23     cin >> M; 24     while(M--) 25  { 26         int K; 27         cin >> K; 28         int len = fun(K); 29         //cout << len << endl;
30         int sum; 31         int ans = -1; 32         for (int i = 1; i < 10; i++) 33  { 34             sum = K*K*i; 35             if(sum%len == K) 36  { 37                 ans = i; 38                 break; 39  } 40  } 41         if(ans == -1) 42  { 43             cout << "No" << endl; 44  } 45         else
46  { 47             cout << ans << " " << sum << endl; 48  } 49  } 50     return 0; 51 }

2. 最好吃的月餅 (20 分)

題目描述:

月餅是久負盛名的中國傳統糕點之一,自唐朝以來,已經發展出幾百品種。

mk.jpg

若想評比出一種“最好吃”的月餅,那勢必在吃貨界引發一場腥風血雨…… 在這里我們用數字說話,給出全國各地各種月餅的銷量,要求你從中找出銷量冠軍,認定為最好吃的月餅。

輸入格式:

輸入首先給出兩個正整數 N(≤1000)和 M(≤100),分別為月餅的種類數(於是默認月餅種類從 1 到 N 編號)和參與統計的城市數量。

接下來 M 行,每行給出 N 個非負整數(均不超過 1 百萬),其中第 i 個整數為第 i 種月餅的銷量(塊)。數字間以空格分隔。

輸出格式:

在第一行中輸出最大銷量,第二行輸出銷量最大的月餅的種類編號。如果冠軍不唯一,則按編號遞增順序輸出並列冠軍。數字間以 1 個空格分隔,行首尾不得有多余空格。

輸入樣例:

5 3
1001 992 0 233 6
8 0 2018 0 2008
36 18 0 1024 4

輸出樣例:

2018
3 5

解題思路:

先建立一個map,map的key值是月餅編號,value值是該月餅的銷量,用ans來記錄最大銷量。創建一個冠軍數組a用來標記冠軍銷量的月餅編號。若某種月餅的銷量大於冠軍月餅的銷量,則把數組a置空重新標記冠軍月餅的編號。最后for循環遍歷數組a輸出值為1的所在下標。

AC代碼:

 1 #include <bits/stdc++.h>
 2 using namespace std;  3 
 4 int main()  5 {  6     map<int,int> m;   //月餅的編號為key,銷量為value
 7     int N,M;  8     cin >> N >> M;  9     int ans = 0;   //最大銷量
10     int a[1005];    //存放冠軍,1為冠軍,0非冠軍
11     memset(a,0,sizeof(a));   //全部初始化為0
12     while(M--) 13  { 14         for(int i=1;i<=N;i++) 15  { 16             int temp; 17             cin >> temp; 18             m[i] += temp; 19             if(m[i] > ans)   //若銷量大於冠軍
20  { 21                 ans = max(ans,m[i]);    //這里可以寫ans = m[i];
22                 memset(a,0,sizeof(a));   //a清零
23                 a[i] = 1; 24  } 25             else if(m[i] == ans)  //若銷量等於冠軍
26  { 27                 if(a[i] == 0)   //判斷是否已經進入冠軍數組
28  { 29                     a[i] = 1;   //要是沒進冠軍數組就標記一下
30  } 31  } 32  } 33  } 34     cout << ans << endl; 35     bool flag = true;    //為了輸出格式而立的flag
36     for ( int i = 1; i < 1005; i++) 37  { 38         if(a[i] != 0) 39  { 40             if(flag) 41  { 42                 flag = false; 43                 cout << i; 44  } 45             else
46  { 47                 cout << " " << i; 48  } 49  } 50  } 51     return 0; 52 }

3. 字符串A+B (20 分)

題目描述:

給定兩個字符串 A 和 B,本題要求你輸出 A+B,即兩個字符串的並集。要求先輸出 A,再輸出 B,但重復的字符必須被剔除。

輸入格式:

輸入在兩行中分別給出 A 和 B,均為長度不超過 10​6​​的、由可見 ASCII 字符 (即碼值為32~126)和空格組成的、由回車標識結束的非空字符串。

輸出格式:

在一行中輸出題面要求的 A 和 B 的和。

輸入樣例:

This is a sample test
to show you_How it works

輸出樣例:

This ampletowyu_Hrk

解題思路:

這個題真的不難,可是這一次PAT乙級考試,我的的確確就輸在了這題上面,在考試的時候由於我時間分配的不合理,我這題沒提交過一次代碼,20分直接沒了。5道題里我就空了這一道水題沒提交過、沒有得分。停止提交之后的5分鍾,我寫出了代碼。這題不就是用map來記錄輸出過的字符,不重復地輸出字符就AC了嗎?可以說是非常遺憾了。

AC代碼:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     string a,b;
 7     getline(cin,a);
 8     getline(cin,b);
 9     string result = a+b;
10     map<char,int> m;    //map的key值是字符,value值用0和1來記錄是否輸出過
11     for(auto it: result)  //for-each循環遍歷字符串A+B
12     {
13         if(m[it]==0)    //若該字符沒有出現過
14         {
15             cout << it;  //輸出該字符
16             m[it] = 1;   //並在map中記錄它輸出過了
17         }
18     }
19     return 0;
20 }

4. 谷歌的招聘 (20 分)

題目描述:

2004 年 7 月,谷歌在硅谷的 101 號公路邊豎立了一塊巨大的廣告牌(如下圖)用於招聘。內容超級簡單,就是一個以 .com 結尾的網址,而前面的網址是一個 10 位素數,這個素數是自然常數 e 中最早出現的 10 位連續數字。能找出這個素數的人,就可以通過訪問谷歌的這個網站進入招聘流程的下一步。

prime.jpg

自然常數 e 是一個著名的超越數,前面若干位寫出來是這樣的:e = 2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427427466391932003059921... 其中粗體標出的 10 位數就是答案。

本題要求你編程解決一個更通用的問題:從任一給定的長度為 L 的數字中,找出最早出現的 K 位連續數字所組成的素數。

輸入格式:

輸入在第一行給出 2 個正整數,分別是 L(不超過 1000 的正整數,為數字長度)和 K(小於 10 的正整數)。接下來一行給出一個長度為 L 的正整數 N。

輸出格式:

在一行中輸出 N 中最早出現的 K 位連續數字所組成的素數。如果這樣的素數不存在,則輸出 404。注意,原始數字中的前導零也計算在位數之內。例如在 200236 中找 4 位素數,0023 算是解;但第一位 2 不能被當成 0002 輸出,因為在原始數字中不存在這個 2 的前導零。

輸入樣例 1:

20 5
23654987725541023819

輸出樣例 1:

49877

輸入樣例 2:

10 3
2468024680

輸出樣例 2:

404

解題思路:

我在考試過程中提交的代碼只有18分,6個測試用例中3和4WA了。當時覺得第3題太水,直接跳第4題寫的(錯誤的決定)。遍歷每個K位string型數字,先用c_str()函數強制轉換成char*型,再用atoi()函數強制轉換成int型判斷它是不是素數。若是素數,把該string型的K位數字賦值給result進行輸出,否則輸出result的初始化字符串404。

18分代碼:

 1 #include <bits/stdc++.h>
 2 using namespace std;  3 
 4 bool isPrime(int n)  5 {  6     if(n <= 1)  7  {  8         return false;  9  } 10     for(int i =2;i<=sqrt(n);i++) 11  { 12         if(n%i == 0) 13  { 14             return false; 15  } 16  } 17     return true; 18 } 19 
20 int main() 21 { 22     int L,K; 23     string N; 24     cin >> L >> K >> N; 25     string result = "404"; 26     for (int i = 0; i < L; i++) 27  { 28         int j = i; 29         string tempstr = ""; 30         for (int j = i; j < i+K; j++) 31  { 32             tempstr += N[j];  //K個連續的數字
33  } 34         int temp = atoi(tempstr.c_str());  //string型的數字先轉成char*再轉成int型的數字
35         if(isPrime(temp))  //若這個數字是素數,則用result來記錄它
36  { 37             result = tempstr; 38             break; 39  } 40  } 41     cout << result << endl; 42     return 0; 43 }

AC代碼:

考試結束之后發現,把上面的18分代碼改成用substr()來得到K位的字符子串就能通過測試用例3、4啦。

 1 #include <bits/stdc++.h>
 2 using namespace std;  3 
 4 bool isPrime(int n)  5 {  6     if(n <= 1)  7  {  8         return false;  9  } 10     for(int i =2;i<=sqrt(n);i++) 11  { 12         if(n%i == 0) 13  { 14             return false; 15  } 16  } 17     return true; 18 } 19 
20 int main() 21 { 22     int L,K; 23     string N; 24     cin >> L >> K >> N; 25     string result = "404"; 26     for (int i = 0; i <= L-K; i++) 27  { 28         string tempstr = N.substr(i,K); 29         int temp = atoi(tempstr.c_str());  //string型的數字先轉成char*再轉成int型的數字
30         if(isPrime(temp))  //若這個數字是素數,則用result來記錄它
31  { 32             result = tempstr; 33             break; 34  } 35  } 36     cout << result << endl; 37     return 0; 38 }

5. 解碼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。

 1 #include <bits/stdc++.h>
 2 using namespace std;  3 
 4 struct stu  5 {  6     string ID;   //准考證號
 7     int score;   //分數
 8 };  9 
 10 bool Cmp(stu a,stu b)  11 {  12     //lambda表達式,先按分數降序排列,若分數相等則按准考證號升序排列
 13     return a.score!=b.score? a.score>b.score : a.ID<b.ID;  14 }  15 
 16 int main()  17 {  18     int N,M;  19     ios::sync_with_stdio(false);   //取消cin和stdin的同步
 20     cin >> N >> M;  21     vector<stu> v(N);  22     for (int i = 0; i < N; i++)  23  {  24         cin >> v[i].ID >> v[i].score;  25  }  26     int xh = 0;  //序號
 27     while(M--)  28  {  29         xh++;  30         bool flag = false;   //false時輸出NA
 31         int lx;     //類型
 32         cin >> lx;  33         if(lx == 1)  34         //類型1要求按分數非升序輸出某個指定級別的考生的成績
 35  {  36             char zl;  37             cin >> zl;  38             cout << "Case " << xh << ": " << lx << " " << zl << endl;  39  sort(v.begin(),v.end(),Cmp);  40             for (auto it = v.begin(); it != v.end(); it++)  41  {  42                 if(it->ID[0] == zl)  43  {  44                     flag = true;  45                     cout << it->ID << " " << it->score << endl;  46  }  47  }  48  }  49         else if(lx == 2)  50         //類型2要求將某指定考場的考生人數和總分統計輸出
 51  {  52             int zl;  53             cin >> zl;  54             cout << "Case " << xh << ": " << lx << " " << zl << endl;  55             int count_r = 0,count_f=0;  //人 分
 56             for (auto it = v.begin(); it != v.end(); it++)  57  {  58                 string tempstr = "";  59                 for (int i = 1; i <= 3; i++)  60  {  61                     tempstr += it->ID[i];  62  }  63                 int temp = atoi(tempstr.c_str());  64                 if(temp == zl)  65  {  66                     count_r++;  67                     count_f += it->score;  68                     flag = true;  69  }  70  }  71             if(!flag)    //查詢結果為空
 72  {  73                 cout << "NA" << endl;  74  }  75             else
 76  {  77                 cout << count_r << " " << count_f << endl;  78  }  79  }  80         else if(lx == 3)  81         //類型3要求將某指定考場的考生人數和總分統計輸出
 82  {  83             string zl;  84             cin >> zl;  85             cout << "Case " << xh << ": " << lx << " " << zl << endl;  86             int a[1000];  87             memset(a,0,sizeof(a));  88             for (auto it = v.begin(); it != v.end(); it++)  89  {  90                 string tempdata = "";  //日期
 91                 for (int i = 4;i <= 9; i++)  92  {  93                     tempdata += it->ID[i];  94  }  95                 if(tempdata == zl)  96  {  97                     flag = true;  98                     string tempstr = "";  //考場
 99                     for(int i=1;i<=3;i++) 100  { 101                         tempstr += it->ID[i]; 102  } 103                     int temp = atoi(tempstr.c_str()); 104                     a[temp]++; 105  } 106  } 107             if(!flag) 108  { 109                 cout << "NA" << endl; 110  } 111             else
112  { 113                 for (int i = 100; i > 0; i--) 114  { 115                     for (int j = 100; j < 1000; j++) 116  { 117                         if(a[j] == i) 118                         cout << j << " " << a[j] << endl; 119  } 120  } 121  } 122 
123  } 124  } 125     return 0; 126 }

19分代碼:

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

 1 #include <bits/stdc++.h>
 2 using namespace std;  3 
 4 struct stu   //考生
 5 {  6     string ID;   //准考證號
 7     int score;   //分數
 8 };  9 
 10 struct room   //考場
 11 {  12     string ID;   //考場編號
 13     int count;    //考場人數
 14 };  15 
 16 bool Cmp1(stu a,stu b)   //類型1用到的比較方法
 17 {  18     //lambda表達式,先按分數降序排列,若分數相等則按准考證號升序排列
 19     return a.score!=b.score? a.score>b.score : a.ID<b.ID;  20 }  21 
 22 bool Cmp3(room a,room b)   //類型3用到的比較方法
 23 {  24     //lambda表達式,先按考場人數降序排列,若考場人數相同則按考場標號升序排列
 25     return a.count!=b.count? a.count>b.count : a.ID<b.ID;  26 }  27 
 28 int main()  29 {  30     int N,M;  31     ios::sync_with_stdio(false);   //取消cin和stdin的同步
 32     cin >> N >> M;  33     vector<stu> v(N);  34     for (int i = 0; i < N; i++)  35  {  36         cin >> v[i].ID >> v[i].score;  37  }  38     int xh = 0;  //序號
 39     while(M--)  40  {  41         xh++;  42         bool flag = false;   //false時輸出NA
 43         int lx;     //類型
 44         cin >> lx;  45         if(lx == 1)  46         //類型1要求按分數非升序輸出某個指定級別的考生的成績
 47  {  48             char zl;  49             cin >> zl;  50             cout << "Case " << xh << ": " << lx << " " << zl << endl;  51  sort(v.begin(),v.end(),Cmp1);  52             for (auto it = v.begin(); it != v.end(); it++)  53  {  54                 if(it->ID[0] == zl)  55  {  56                     flag = true;  57                     cout << it->ID << " " << it->score << endl;  58  }  59  }  60             if(!flag)  61  {  62                 cout << "NA" << endl;  63  }  64  }  65         else if(lx == 2)  66         //類型2要求將某指定考場的考生人數和總分統計輸出
 67  {  68             int zl;  69             cin >> zl;  70             cout << "Case " << xh << ": " << lx << " " << zl << endl;  71             int count_r = 0,count_f=0;  //人 分
 72             for (auto it = v.begin(); it != v.end(); it++)  73  {  74                 string tempstr = it->ID.substr(1,3);  75                 int temp = atoi(tempstr.c_str());  76                 if(temp == zl)  77  {  78                     count_r++;  79                     count_f += it->score;  80                     flag = true;  81  }  82  }  83             if(!flag)    //查詢結果為空
 84  {  85                 cout << "NA" << endl;  86  }  87             else
 88  {  89                 cout << count_r << " " << count_f << endl;  90  }  91  }  92         else if(lx == 3)  93         //類型3要求將某指定考場的考生人數和總分統計輸出
 94  {  95             string zl;  96             cin >> zl;  97             cout << "Case " << xh << ": " << lx << " " << zl << endl;  98             vector<room> kc;  99             map<string,int> m; 100             for (auto it = v.begin(); it != v.end(); it++) 101  { 102                 if(it->ID.substr(4,6) == zl) 103  { 104                     flag = true; 105                     string tempstr = it->ID.substr(1,3);  //考場 106                     //int temp = atoi(tempstr.c_str());
107                     m[tempstr]++; 108  } 109  } 110             for (auto it : m)   //把map全部推入vector中來排序
111  { 112  kc.push_back({it.first,it.second}); 113  } 114  sort(kc.begin(),kc.end(),Cmp3); 115             if(!flag) 116  { 117                 cout << "NA" << endl; 118  } 119             else
120  { 121                 for (int i = 0; i < kc.size(); i++) 122  { 123                     cout << kc[i].ID  << " " << kc[i].count << endl; 124  } 125  } 126  } 127  } 128     return 0; 129 }

22分代碼:

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

 1 #include <bits/stdc++.h>
 2 using namespace std;  3  
 4 struct stu   //考生
 5 {  6     string ID;   //准考證號
 7     int score;   //分數
 8 };  9  
 10 struct room   //考場
 11 {  12     string ID;   //考場編號
 13     int count;    //考場人數
 14 };  15  
 16 bool Cmp1(stu &a,stu &b)   //類型1用到的比較方法
 17 {  18     //lambda表達式,先按分數降序排列,若分數相等則按准考證號升序排列
 19     return a.score!=b.score? a.score>b.score : a.ID<b.ID;  20 }  21  
 22 bool Cmp3(room &a,room &b)   //類型3用到的比較方法
 23 {  24     //lambda表達式,先按考場人數降序排列,若考場人數相同則按考場標號升序排列
 25     return a.count!=b.count? a.count>b.count : a.ID<b.ID;  26 }  27  
 28 int main()  29 {  30     int N,M;  31     ios::sync_with_stdio(false);   //取消cin和stdin的同步
 32     cin >> N >> M;  33     vector<stu> v(N);  34     for (int i = 0; i < N; i++)  35  {  36         cin >> v[i].ID >> v[i].score;  37  }  38     int xh = 0;  //序號
 39     while(M--)  40  {  41         xh++;  42         bool flag = false;   //false時輸出NA
 43         int lx;     //類型
 44         cin >> lx;  45         if(lx == 1)  46         //類型1要求按分數非升序輸出某個指定級別的考生的成績
 47  {  48             char zl;  49             cin >> zl;  50             cout << "Case " << xh << ": " << lx << " " << zl << endl;  51  sort(v.begin(),v.end(),Cmp1);  52             for (auto it = v.begin(); it != v.end(); it++)  53  {  54                 if(it->ID[0] == zl)  55  {  56                     flag = true;  57                     cout << it->ID << " " << it->score << endl;  58  }  59  }  60             if(!flag)  61  {  62                 cout << "NA" << endl;  63  }  64  }  65         else if(lx == 2)  66         //類型2要求將某指定考場的考生人數和總分統計輸出
 67  {  68             int zl;  69             cin >> zl;  70             cout << "Case " << xh << ": " << lx << " " << zl << endl;  71             int count_r = 0,count_f=0;  //人 分
 72             for (auto it = v.begin(); it != v.end(); it++)  73  {  74                 string tempstr = it->ID.substr(1,3);  75                 int temp = atoi(tempstr.c_str());  76                 if(temp == zl)  77  {  78                     count_r++;  79                     count_f += it->score;  80                     flag = true;  81  }  82  }  83             if(!flag)    //查詢結果為空
 84  {  85                 cout << "NA" << endl;  86  }  87             else
 88  {  89                 cout << count_r << " " << count_f << endl;  90  }  91  }  92         else if(lx == 3)  93         //類型3要求將某指定考場的考生人數和總分統計輸出
 94  {  95             string zl;  96             cin >> zl;  97             cout << "Case " << xh << ": " << lx << " " << zl << endl;  98             vector<room> kc;  99             map<string,int> m; 100             for (auto it = v.begin(); it != v.end(); it++) 101  { 102                 if(it->ID.substr(4,6) == zl) 103  { 104                     flag = true; 105                     string tempstr = it->ID.substr(1,3);  //考場 106                     //int temp = atoi(tempstr.c_str());
107                     m[tempstr]++; 108  } 109  } 110             for (auto it : m)   //把map全部推入vector中來排序
111  { 112  kc.push_back({it.first,it.second}); 113  } 114  sort(kc.begin(),kc.end(),Cmp3); 115             if(!flag) 116  { 117                 cout << "NA" << endl; 118  } 119             else
120  { 121                 for (int i = 0; i < kc.size(); i++) 122  { 123                     cout << kc[i].ID  << " " << kc[i].count << endl; 124  } 125  } 126  } 127  } 128     return 0; 129 }

AC代碼:

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

 1 #include <bits/stdc++.h>
 2 using namespace std;  3 
 4 struct stu   //考生
 5 {  6     string ID;   //准考證號
 7     int score;   //分數
 8 };  9 
 10 struct room   //考場
 11 {  12     string ID;   //考場編號
 13     int count;    //考場人數
 14 };  15 
 16 bool Cmp1(stu &a,stu &b)   //類型1用到的比較方法
 17 {  18     //lambda表達式,先按分數降序排列,若分數相等則按准考證號升序排列
 19     return a.score!=b.score? a.score>b.score : a.ID<b.ID;  20 }  21 
 22 bool Cmp3(room &a,room &b)   //類型3用到的比較方法
 23 {  24     //lambda表達式,先按考場人數降序排列,若考場人數相同則按考場標號升序排列
 25     return a.count!=b.count? a.count>b.count : a.ID<b.ID;  26 }  27 
 28 int main()  29 {  30     int N,M;  31     ios::sync_with_stdio(false);   //取消cin和stdin的同步
 32     cin >> N >> M;  33     vector<stu> v(N);  34     for (int i = 0; i < N; i++)  35  {  36         cin >> v[i].ID >> v[i].score;  37  }  38     int xh = 0;  //序號
 39     while(M--)  40  {  41         xh++;  42         bool flag = false;   //false時輸出NA
 43         int lx;     //類型
 44         cin >> lx;  45         if(lx == 1)  46         //類型1要求按分數非升序輸出某個指定級別的考生的成績
 47  {  48             char zl;  49             cin >> zl;  50             printf("Case %d: %d %c\n", xh, lx, zl);  51  sort(v.begin(),v.end(),Cmp1);  52             for (auto it = v.begin(); it != v.end(); it++)  53  {  54                 if(it->ID[0] == zl)  55  {  56                     flag = true;  57                     printf("%s %d\n", it->ID.c_str(), it->score);  58  }  59  }  60             if(!flag)  61  {  62                 printf("NA\n");  63  }  64  }  65         else if(lx == 2)  66         //類型2要求將某指定考場的考生人數和總分統計輸出
 67  {  68             int zl;  69             cin >> zl;  70             printf("Case %d: %d %d\n", xh, lx, zl);  71             int count_r = 0,count_f=0;  //人 分
 72             for (auto it = v.begin(); it != v.end(); it++)  73  {  74                 string tempstr = it->ID.substr(1,3);  75                 int temp = atoi(tempstr.c_str());  76                 if(temp == zl)  77  {  78                     count_r++;  79                     count_f += it->score;  80                     flag = true;  81  }  82  }  83             if(!flag)    //查詢結果為空
 84  {  85                 printf("NA\n");  86  }  87             else
 88  {  89                 printf("%d %d\n", count_r, count_f);  90  }  91  }  92         else if(lx == 3)  93         //類型3要求將某指定考場的考生人數和總分統計輸出
 94  {  95             string zl;  96             cin >> zl;  97             printf("Case %d: %d %s\n", xh, lx, zl.c_str());  98             vector<room> kc;  99             unordered_map<string,int> m; 100             for (auto it = v.begin(); it != v.end(); it++) 101  { 102                 if(it->ID.substr(4,6) == zl) 103  { 104                     flag = true; 105                     string tempstr = it->ID.substr(1,3);  //考場 106                     //int temp = atoi(tempstr.c_str());
107                     m[tempstr]++; 108  } 109  } 110             for (auto it : m)   //把map全部推入vector中來排序
111  { 112  kc.push_back({it.first,it.second}); 113  } 114  sort(kc.begin(),kc.end(),Cmp3); 115             if(!flag) 116  { 117                 printf("NA\n"); 118  } 119             else
120  { 121                 for (int i = 0; i < kc.size(); i++) 122  { 123                     printf("%s %d\n", kc[i].ID.c_str(), kc[i].count); 124  } 125  } 126  } 127  } 128     return 0; 129 }

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM