国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

PAT乙級 (Basic Level) Practise - 1015 德才論

2019-11-10 19:06:08
字體:
來源:轉載
供稿:網友

1015. 德才論 (25)

原題:

宋代史學家司馬光在《資治通鑒》中有一段著名的“德才論”:“是故才德全盡謂之圣人,才德兼亡謂之愚人,德勝才謂之君子,才勝德謂之小人。凡取人之術,茍不得圣人,君子而與之,與其得小人,不若得愚人。”

現給出一批考生的德才分數,請根據司馬光的理論給出錄取排名。

輸入格式:

輸入第1行給出3個正整數,分別為:N(<=105),即考生總數;L(>=60),為錄取最低分數線,即德分和才分均不低于L的考生才有資格被考慮錄取;H(<100),為優先錄取線——德分和才分均不低于此線的被定義為“才德全盡”,此類考生按德才總分從高到低排序;才分不到但德分到線的一類考生屬于“德勝才”,也按總分排序,但排在第一類考生之后;德才分均低于H,但是德分不低于才分的考生屬于“才德兼亡”但尚有“德勝才”者,按總分排序,但排在第二類考生之后;其他達到最低線L的考生也按總分排序,但排在第三類考生之后。

隨后N行,每行給出一位考生的信息,包括:準考證號、德分、才分,其中準考證號為8位整數,德才分為區間[0, 100]內的整數。數字間以空格分隔。

輸出格式:

輸出第1行首先給出達到最低分數線的考生人數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

思路:

該題首先要對輸入數據進行分類,再進行多級排序。思路上清晰,但具體實現很繁瑣。我是用純C語言實現的,起先自己試寫多級排序函數,但是提交后提示有兩個測試節點運行超時,懷疑是大量數據輸入時導致排序函數速度更不上而超時。

很頭疼的小改了下代碼,還是超時。于是參考了人家寫的C/C++混合代碼,發現大多存在這個問題,且過來人均建議采用stdlib函數庫中的qsort來實現該排序功能。于是我學習并采用了該函數,使得最終代碼成功過檢。

總之該題耗費了很多時間,但是也學到了不少東西。

原始代碼(DIY的多級排序:超時):

#include <stdio.h>#include <stdlib.h>struct stu{ int ID; int D; int C; int S;};void sort_func(struct stu* class, int num);int main(void){ int N, L, H; int i; struct stu *stud; struct stu *class1, *class2, *class3, *class4; int cnt1 = 0, cnt2 = 0, cnt3 = 0, cnt4 = 0; scanf("%d%d%d", &N, &L, &H); while(N<=0) { 最終代碼(采用庫函數:成功過檢)

#include <stdio.h>#include <stdlib.h>struct stu{ int ID; int D; int C; int S;};int cmp(const void *p1, const void *p2){ struct stu *x = (struct stu *)p1; struct stu *y = (struct stu *)p2; if (x->S!=y->S) return x->S < y->S; else if(x->D!=y->D) return x->D < y->D; else return x->ID > y->ID;}int main(void){ int N, L, H; int i; struct stu *stud; struct stu *class1, *class2, *class3, *class4; int cnt1 = 0, cnt2 = 0, cnt3 = 0, cnt4 = 0; scanf("%d%d%d", &N, &L, &H); while(N<=0) { printf("INPUT ILLEGAL!! PLEASE INPUT AGAIN!!/n"); scanf("%d%d%d", &N, &L, &H); } stud = (struct stu *)malloc(sizeof(struct stu)*N); class1 = (struct stu *)malloc(sizeof(struct stu)*N); class2 = (struct stu *)malloc(sizeof(struct stu)*N); class3 = (struct stu *)malloc(sizeof(struct stu)*N); class4 = (struct stu *)malloc(sizeof(struct stu)*N); for(i = 0; i < N; i++) { scanf("%d%d%d", &stud[i].ID, &stud[i].D, &stud[i].C); if(stud[i].D<L && stud[i].C<L) continue; if (stud[i].D>=H && stud[i].C>=H) { class1[cnt1] = stud[i]; class1[cnt1].S = class1[cnt1].D + class1[cnt1].C; cnt1++; } else if(stud[i].D>=H && stud[i].C<H && stud[i].C>=L) { class2[cnt2] = stud[i]; class2[cnt2].S = class2[cnt2].D + class2[cnt2].C; cnt2++; } else if(stud[i].D<H && stud[i].C<H && stud[i].D>=stud[i].C && stud[i].D>=L && stud[i].C>=L) { class3[cnt3] = stud[i]; class3[cnt3].S = class3[cnt3].D + class3[cnt3].C; cnt3++; } else if(stud[i].D>=L && stud[i].C>=L) { class4[cnt4] = stud[i]; class4[cnt4].S = class4[cnt4].D + class4[cnt4].C; cnt4++; } } qsort(class1, cnt1, sizeof(class1[0]), cmp); qsort(class2, cnt2, sizeof(class2[0]), cmp); qsort(class3, cnt3, sizeof(class3[0]), cmp); qsort(class4, cnt4, sizeof(class4[0]), cmp); printf("%d/n", cnt1+cnt2+cnt3+cnt4); for (i = 0; i < cnt1; ++i) { printf("%d %d %d", class1[i].ID, class1[i].D, class1[i].C); if (i!=(cnt1-1) || (cnt2+cnt3+cnt4)) { printf("/n"); } } for (i = 0; i < cnt2; ++i) { printf("%d %d %d", class2[i].ID, class2[i].D, class2[i].C); if (i!=cnt2-1 || (cnt3+cnt4)) { printf("/n"); } } for (i = 0; i < cnt3; ++i) { printf("%d %d %d", class3[i].ID, class3[i].D, class3[i].C); if (i!=cnt3-1 || cnt4) { printf("/n"); } } for (i = 0; i < cnt4; ++i) { printf("%d %d %d", class4[i].ID, class4[i].D, class4[i].C); if (i!=cnt4-1) { printf("/n"); } } free(stud); free(class1); free(class2); free(class3); free(class4); return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 同江市| 汝城县| 莱阳市| 滕州市| 清镇市| 克拉玛依市| 辽宁省| 邛崃市| 蓬莱市| 夏津县| 灵武市| 长丰县| 长白| 吴川市| 庆安县| 沙田区| 红原县| 太谷县| 于都县| 林口县| 遵化市| 融水| 邢台县| 富宁县| 维西| 瓦房店市| 临武县| 天津市| 辽中县| 筠连县| 沈丘县| 通化县| 崇文区| 三河市| 丹江口市| 上林县| 云林县| 镇赉县| 南丰县| 开封县| 安吉县|