1. 程式人生 > 實用技巧 >*PAT(Basic Level) 1015 德才論

*PAT(Basic Level) 1015 德才論

1015 德才論 (25分)
宋代史學家司馬光在《資治通鑑》中有一段著名的“德才論”:“是故才德全盡謂之聖人,才德兼亡謂之愚人,德勝才謂之君子,才勝德謂之小人。
凡取人之術,苟不得聖人,君子而與之,與其得小人,不若得愚人。” 現給出一批考生的德才分數,請根據司馬光的理論給出錄取排名。 輸入格式: 輸入第一行給出
3 個正整數,分別為:N(≤10^5),即考生總數;L(≥60),為錄取最低分數線,即德分和才分均不低於 L 的考生才有資格被考慮錄取;
H(<100),為優先錄取線——德分和才分均不低於此線的被定義為“才德全盡”,此類考生按德才總分從高到低排序;
才分不到但德分到線的一類考生屬於“德勝才”,也按總分排序,但排在第一類考生之後;
德才分均低於 H,但是德分不低於才分的考生屬於“才德兼亡”但尚有“德勝才”者,按總分排序,但排在第二類考生之後;
其他達到最低線 L 的考生也按總分排序,但排在第三類考生之後。 隨後 N 行,每行給出一位考生的資訊,包括:准考證號 德分 才分,其中准考證號為
8 位整數,德才分為區間 [0, 100] 內的整數。數字間以空格分隔。 輸出格式: 輸出第一行首先給出達到最低分數線的考生人數 M,隨後 M 行,每行按照輸入格式輸出一位考生的資訊,考生按輸入中說明的規則從高到低排序。
當某類考生中有多人總分相同時,按其德分降序排列;
若德分也並列,則按准考證號的升序輸出。 輸入樣例:
14 60 80 10000001 64 90 10000002 90 60 10000011 85 80 10000003 85 80 10000004 80 85 10000005 82 77 10000006 83 76 10000007 90 78 10000008 75 79 10000009 59 90 10000010 88 45 10000012 80 100 10000013 90 99 10000014 66 60 輸出樣例: 12 10000013 90 99 10000012 80 100 10000003 85 80 10000011 85 80 10000004 80 85 10000007 90 78 10000006 83 76 10000005 82 77 10000002 90 60 10000014 66 60 10000008 75 79 10000001 64 90

 1 #include<iostream>
 2
#include<stdio.h> 3 #include<string> 4 #include<vector> 5 #include<algorithm> 6 7 using namespace std; 8 9 struct node 10 { 11 int id; 12 int df,cf,zf; 13 }; 14 15 int cmp(node a,node b) 16 { 17 return a.zf != b.zf ? a.zf > b.zf :(a.df != b.df ? a.df > b.df : a.id < b.id); 18 } 19 20 int main() 21 { 22 int n,l,h; 23 // cin>>n>>l>>h; 24 scanf("%d %d %d",&n,&l,&h); 25 26 vector<node> student[4]; 27 int time = 0;  //計數 28 for(int i = 0; i < n; i++ ) 29 { 30 node temp; 31 //cin>>temp.id>>temp.df>>temp.cf; 32 scanf("%d %d %d",&temp.id,&temp.df,&temp.cf); 33 temp.zf = temp.df + temp.cf; 34 35 if(temp.cf >= l && temp.df >= l) 36 { 37 if(temp.cf >= h && temp.df >= h) 38 student[0].push_back(temp); 39 else if(temp.df >= h && temp.cf < h) 40 student[1].push_back(temp); 41 else if(temp.df < h && temp.cf < h && temp.df >= temp.cf) 42 student[2].push_back(temp); 43 else 44 student[3].push_back(temp); 45 time++; 46 } 47 } 48 //cout<<time<<endl; //輸出達到最低分數線的考生人數 49 printf("%d\n",time); 50 51 for(int i = 0; i < 4; i++ ) 52 { 53 sort(student[i].begin(),student[i].end(),cmp); 54 for(int j = 0; j < student[i].size(); j++ ) 55 // cout<<student[i][j].id<<' '<<student[i][j].df<<' '<<student[i][j].cf<<endl; 56    printf("%d %d %d\n",student[i][j].id,student[i][j].df,student[i][j].cf); 57 } 58 59 return 0; 60 }

參考:https://blog.csdn.net/guangjinzheng/article/details/80514303以及另一篇部落格(懶得找網址了,真是優美的程式碼啊)⬆⬆⬆

這位博主說“用C++的cin和cout輸入輸出會超時,但是用C的scanf和printf就不會超時”。

確實是這樣。

有點好奇為什麼會這樣,先把問題放在這兒。

學習筆記1:

#include<iostream>

using namespace std;

int main()
{
    int a = 2;
    int b = 4;
    int i = a > b;
    int x = a < b;

    cout << i << endl;        //輸出為0
    cout << x << endl;    //輸出為1
    

    return 0;
}

學習筆記2:

關於vector

可以簡單的認為,向量是一個能夠存放任意型別的動態陣列

push_back:在陣列的最後新增一個數據。

https://blog.csdn.net/qq_36568418/article/details/101536794

(這篇部落格後半部分講push_back原理的暫時無法理解,先放在這兒,到時候記得看。)

另:

  • 可以通過使用 * vector.begin() 或 * vector.end() 來獲得 vector 中第一個或最後一個的值;
  • 也可以直接使用 vector.front() 、vector.back() 來得到 vector 首尾的值

所以vector.begin()和vector.end()返回的是地址咯?(確實如此,返回了指標)。

vector用法總結:https://www.cnblogs.com/zhonghuasong/p/5975979.html

學習筆記3:

c++標準函式庫algorithmsort函式

https://blog.csdn.net/qq_36568418/article/details/101536794

(這篇部落格介紹了sort函式的使用方法。)

總結一下:

sort函式只有兩個引數時,傳入的是首地址和末地址,預設從小到大排序;

sort函式也可以自定義排序規則,需要傳入第三個引數,這個引數為定義排序規則的函式名。