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

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

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

2019-11-10 21:35:01
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

1015. 德才論 (25)

原題:

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

現(xiàn)給出一批考生的德才分?jǐn)?shù),請(qǐng)根據(jù)司馬光的理論給出錄取排名。

輸入格式:

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

隨后N行,每行給出一位考生的信息,包括:準(zhǔn)考證號(hào)、德分、才分,其中準(zhǔn)考證號(hào)為8位整數(shù),德才分為區(qū)間[0, 100]內(nèi)的整數(shù)。數(shù)字間以空格分隔。

輸出格式:

輸出第1行首先給出達(dá)到最低分?jǐn)?shù)線的考生人數(shù)M,隨后M行,每行按照輸入格式輸出一位考生的信息,考生按輸入中說(shuō)明的規(guī)則從高到低排序。當(dāng)某類考生中有多人總分相同時(shí),按其德分降序排列;若德分也并列,則按準(zhǔn)考證號(hào)的升序輸出。 輸入樣例:

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

思路:

該題首先要對(duì)輸入數(shù)據(jù)進(jìn)行分類,再進(jìn)行多級(jí)排序。思路上清晰,但具體實(shí)現(xiàn)很繁瑣。我是用純C語(yǔ)言實(shí)現(xiàn)的,起先自己試寫(xiě)多級(jí)排序函數(shù),但是提交后提示有兩個(gè)測(cè)試節(jié)點(diǎn)運(yùn)行超時(shí),懷疑是大量數(shù)據(jù)輸入時(shí)導(dǎo)致排序函數(shù)速度更不上而超時(shí)。

很頭疼的小改了下代碼,還是超時(shí)。于是參考了人家寫(xiě)的C/C++混合代碼,發(fā)現(xiàn)大多存在這個(gè)問(wèn)題,且過(guò)來(lái)人均建議采用stdlib函數(shù)庫(kù)中的qsort來(lái)實(shí)現(xiàn)該排序功能。于是我學(xué)習(xí)并采用了該函數(shù),使得最終代碼成功過(guò)檢。

總之該題耗費(fèi)了很多時(shí)間,但是也學(xué)到了不少東西。

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

#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) { 最終代碼(采用庫(kù)函數(shù):成功過(guò)檢)

#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;}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 迁安市| 改则县| 丰顺县| 滨州市| 浦江县| 福建省| 青阳县| 泸州市| 老河口市| 塔城市| 南华县| 台安县| 重庆市| 宁安市| 长顺县| 柞水县| 金溪县| 双桥区| 阿图什市| 镇平县| 嵊州市| 曲周县| 武川县| 杨浦区| 大丰市| 新绛县| 石嘴山市| 内丘县| 阿坝县| 南乐县| 高阳县| 巴青县| 兰坪| 通城县| 新源县| 留坝县| 永德县| 赤壁市| 黑山县| 四川省| 东丰县|