About 900 years ago, a Chinese philosopher Sima Guang wrote a history book in which he talked about people's talent and virtue. According to his theory, a man being outstanding in both talent and virtue must be a "sage(聖人)"; being less excellent but with one's virtue outweighs talent can be called a "nobleman(君子)"; being good in neither is a "fool man(愚人)"; yet a fool man is better than a "small man(小人)" who prefers talent than virtue.html
Now given the grades of talent and virtue of a group of people, you are supposed to rank them according to Sima Guang's theory.git
Each input file contains one test case. Each case first gives 3 positive integers in a line: N (≤105), the total number of people to be ranked; L (≥60), the lower bound of the qualified grades -- that is, only the ones whose grades of talent and virtue are both not below this line will be ranked; and H (<100), the higher line of qualification -- that is, those with both grades not below this line are considered as the "sages", and will be ranked in non-increasing order according to their total grades. Those with talent grades below H but virtue grades not are cosidered as the "noblemen", and are also ranked in non-increasing order according to their total grades, but they are listed after the "sages". Those with both grades below H, but with virtue not lower than talent are considered as the "fool men". They are ranked in the same way but after the "noblemen". The rest of people whose grades both pass the L line are ranked after the "fool men".數組
Then N lines follow, each gives the information of a person in the format:less
ID_Number Virtue_Grade Talent_Grade
where ID_Number
is an 8-digit number, and both grades are integers in [0, 100]. All the numbers are separated by a space.ide
The first line of output must give M (≤N), the total number of people that are actually ranked. Then M lines follow, each gives the information of a person in the same format as the input, according to the ranking rules. If there is a tie of the total grade, they must be ranked with respect to their virtue grades in non-increasing order. If there is still a tie, then output in increasing order of their ID's.優化
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
第一行給出三個整數:考生人數N,最低錄取線L,優先錄取線Hthis
德分和才分均不低於L的纔有錄取資格spa
德才分均不低於H的爲I類,最優先錄取rest
德分不低於H但才分低於H的爲II類,次優先錄取excel
德分和才分均低於H但德分不低於才分的爲III類,更次錄取
其他考生爲IV類,最次優先錄取
①按類別從低到高排序
②類別相同,按總分從高到低排序
③總分相同,按德分從高到低排序
④德分相同,按考號從低到高排序
二、所須要的信息及信息的存儲
①對於單個學生student,須要對應的准考證號id,德分de,才分cai,總分sum,類別flag——結構體Student,並建立一個足夠大的結構體數組
②此外還須要總考生數n,最低分數線l,優先錄取分數線h,錄取考生數num——int型變量
1 bool cmp(Student a, Student b) { 2 if (a.flag != b.flag) { 3 return a.flag < b.flag; 4 } else if (a.sum != b.sum) { 5 return a.sum > b.sum; 6 } else if (a.de != b.de) { 7 return a.de > b.de; 8 } else { 9 return strcmp(a.id, b.id) < 0; 10 } 11 }
int main() { int n, l, h, num = 0; scanf("%d%d%d", &n, &l, &h); for (int i = 0; i < n; i++) { scanf("%s%d%d", stu[i].id, &stu[i].de, &stu[i].cai); stu[i].sum = stu[i].de + stu[i].cai; if (stu[i].de < l || stu[i].cai < l) { stu[i].flag = 5; } else { num++; if (stu[i].de >= h && stu[i].cai >= h) { stu[i].flag = 1; } else if (stu[i].de >= h && stu[i].cai < h) { stu[i].flag = 2; } else if (stu[i] < h && stu[i].cai < h && stu[i].de >= stu[i].cai) { stu[i].flag = 3; } else { stu[i].flag = 4; } } } sort(stu, stu + n, cmp); }
①邏輯優化
德分 < L | L <= 德分 < H | 德分 >= H | |
才分 < L | 5 | 5 | 5 |
L <= 才分 < H | 5 | 3 | 2 |
才分 >= H | 5 | 4 | 1 |
因此,if-else邏輯應簡化爲
1 if (stu[i].de < l || stu[i].cai < l) { 2 stu[i].flag = 5; 3 } else if (stu[i].de >= h && stu[i].cai >= h) { 4 stu[i].flag = 1; 5 } else if (stu[i].de >= h && stu[i].cai < h) { 6 stu[i].flag = 2; 7 } else if (stu[i].de >= stu[i].cai) { 8 stu[i].flag = 3; 9 } else { 10 stu[i].flag = 4; 11 }
②num的替換
若在①中後四個else if中,每一個都寫上num++,會顯得過於繁雜
所以將num初值設爲參與考試的人數n,每次出現不合格的人時num--
1 int num = n; 2 3 if (stu[i].de < l || stu[i].cai < l) { 4 stu[i].flag = 5; 5 num--; 6 }
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 struct Student { 6 char id[10]; 7 int de; 8 int cai; 9 int sum; 10 int flag; 11 }stu[100010]; 12 bool cmp(Student a, Student b) { 13 if (a.flag != b.flag) { 14 return a.flag < b.flag; 15 } else if (a.sum != b.sum) { 16 return a.sum > b.sum; 17 } else if (a.de != b.de) { 18 return a.de > b.de; 19 } else { 20 return strcmp(a.id, b.id) < 0; 21 } 22 } 23 int main() { 24 int n, l, h; 25 scanf("%d%d%d", &n, &l, &h); 26 int num = n; 27 for (int i = 0; i < n; i++) { 28 scanf("%s%d%d", stu[i].id, &stu[i].de, &stu[i].cai); 29 stu[i].sum = stu[i].de + stu[i].cai; 30 if (stu[i].de < l || stu[i].cai < l) { 31 stu[i].flag = 5; 32 num--; 33 } else if (stu[i].de >= h && stu[i].cai >= h) { 34 stu[i].flag = 1; 35 } else if (stu[i].de >= h && stu[i].cai < h) { 36 stu[i].flag = 2; 37 } else if (stu[i].de >= stu[i].cai) { 38 stu[i].flag = 3; 39 } else { 40 stu[i].flag = 4; 41 } 42 } 43 sort(stu, stu + n, cmp); 44 printf("%d\n", num); 45 for (int i = 0; i < num; i++) { 46 printf("%s %d %d\n", stu[i].id, stu[i].de, stu[i].cai); 47 } 48 }
題目中准考證號給出8位
但實際使用char[8]存儲再輸出時會出錯???