描述
宋代史學(xué)家司馬光在《資治通鑒》中有一段著名的“德才論”:“是故才德全盡謂之圣人,才德兼亡謂之愚人,德勝才謂之君子,才勝德謂之小人。凡取人之術(shù),茍不得圣人,君子而與之,與其得小人,不若得愚人?!?/p>
現(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
不會(huì)做, copy回來(lái)慢慢理解
#include <stdio.h>
#include <stdlib.h>
typedef struct info_ {
int id;
int de;
int cai;
int sum; // 總分
int type;
} Info;
int get_type(int de, int cai);
int compar(const void *pa, const void *pb);
int high, low;
int main(void)
{
int size;
scanf("%d %d %d", &size, &low, &high);
Info *xs = (Info*)malloc(size * sizeof(Info));
int count = 0;
int i;
for (i=0; i<size; i++){
scanf("%d %d %d", &xs[i].id, &xs[i].de, &xs[i].cai);
xs[i].sum = xs[i].de + xs[i].cai;
xs[i].type = get_type(xs[i].de, xs[i].cai);
if (xs[i].type != 5) {
count++;
}
}
printf("%d\n", count);
qsort(xs, size, sizeof(Info), compar);
for (i=0; i<count; i++){
printf("%d %d %d\n", xs[i].id, xs[i].de, xs[i].cai);
printf("type=%d, sum=%d, de=%d\n", xs[i].type, xs[i].sum, xs[i].de);
}
free(xs);
return 0;
}
int compar(const void *pa, const void *pb)
{
Info a = *(Info *)pa;
Info b = *(Info *)pb;
int ret;
if (a.type == b.type){
if (a.sum == b.sum){
if (a.de == b.de){
ret = a.id - b.id;
} else {
ret = b.de - a.de;
}
} else {
ret = b.sum - a.sum;
}
} else {
ret = a.type - b.type;
}
return ret;
}
int get_type(int de, int cai)
{
int type;
if (de >= high){
if (cai >= high){ // 徳才優(yōu)秀
type = 1;
} else if (cai >= low) { // 徳優(yōu)秀,才及格
type = 2;
} else { // 徳優(yōu)秀,才不及格
type = 5;
}
} else if ( de >= low){
if (cai >= high){ // 徳及格,才優(yōu)秀
type = 4;
} else if (cai >= low){
if (de >= cai){ // 才德及格,但是徳大于才
type = 3;
} else { // 徳才及格
type = 4;
}
} else { // 徳及格才不及格
type = 5;
}
} else { // 徳不及格
type = 5;
}
return type;
}