九度題目1324:The Best Rank
題目1324:The Best Rank
時間限製:1 秒
內存限製:32 兆
特殊判題:否
提交:414
解決:107
題目描述:
To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C
Programming Language, M - Mathematics (Calculus or Linear Algrbra), and E - English. At the mean time, we encourage students by
emphasizing on their best ranks -- that is, among the four ranks with respect to the three courses and the average grade, we print
the best rank for each student.
For example, The grades of C, M, E and A - Average of 4 students are given as the following:
StudentID C M E A
310101 98 85 88 90
310102 70 95 88 84
310103 82 87 94 88
310104 91 91 91 91
Then the best ranks for all the students are No.1 since the 1st one has done the best in C Programming Language, while the 2nd one
in Mathematics, the 3rd one in English, and the last one in average.
輸入:
Each input file contains one test case. Each case starts with a line containing 2 numbers N and M (<=2000), which are the total
number of students, and the number of students who would check their ranks, respectively. Then N lines follow, each contains a
student ID which is a string of 6 digits, followed by the three integer grades (in the range of [0, 100]) of that student in the
order of C, M and E. Then there are M lines, each containing a student ID.
輸出:
For each of the M students, print in one line the best rank for him/her, and the symbol of the corresponding rank, separated by a
space.
The priorities of the ranking methods are ordered as A > C > M > E. Hence if there are two or more ways for a student to obtain the
same best rank, output the one with the highest priority.
If a student is not on the grading list, simply output "N/A".
樣例輸入:
5 6
310101 98 85 88
310102 70 95 88
310103 82 87 94
310104 91 91 91
310105 85 90 90
310101
310102
310103
310104
310105
999999樣例輸出:
1 C
1 M
1 E
1 A
3 A
N/A
代碼:
/*二維數組的橫縱向排序問題,用到了結構體,注意成績相同 同學的排名,題很水,但是比較麻煩,特別適合馬虎的人做(我就是)*/ #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; struct node { int ID,C,M,E,A;//學生的編號,三科的成績和平均成績 int PC,PM,PE,PA;//這幾科的排名 int con;//最高排名的成績 char val;//最高排名的課的名字 }stu[2500]; int cmp1(node a,node b)//按C成績排序 { if(a.C!=b.C) return a.C>b.C; } int cmp2(node a,node b)//按M成績排序 { if(a.M!=b.M) return a.M>b.M; } int cmp3(node a,node b)//按E成績排序 { if(a.E!=b.E) return a.E>b.E; } int cmp4(node a,node b)//按平均成績排序 { if(a.A!=b.A) return a.A>b.A; } int min(int n,int m) {return n<m?n:m;} int main() { int i,j,n,m,x; while(scanf("%d %d",&n,&m)!=EOF) { memset(stu,0,sizeof(stu)); for(i=0;i<n;i++) { scanf("%d %d %d %d",&stu[i].ID,&stu[i].C,&stu[i].M,&stu[i].E); stu[i].A=(stu[i].C+stu[i].M+stu[i].E)/3; } //假設四個人的C成績為 90 90 80 70 則排名為 1 1 3 4 //所以注意成績一樣的同學,排名是一樣的(第三個人的排名是3而不是二) sort(stu,stu+n,cmp1); for(i=0;i<n;i++) { if(i==0) stu[i].PC=i+1; if(i!=0&&stu[i].C!=stu[i-1].C) stu[i].PC=i+1; if(i!=0&&stu[i].C==stu[i-1].C) stu[i].PC=stu[i-1].PC; } sort(stu,stu+n,cmp2); for(i=0;i<n;i++) { if(i==0) stu[i].PM=i+1; if(i!=0&&stu[i].M!=stu[i-1].M) stu[i].PM=i+1; if(i!=0&&stu[i].M==stu[i-1].M) stu[i].PM=stu[i-1].PM; } sort(stu,stu+n,cmp3); for(i=0;i<n;i++) { if(i==0) stu[i].PE=i+1; if(i!=0&&stu[i].E!=stu[i-1].E) stu[i].PE=i+1; if(i!=0&&stu[i].E==stu[i-1].E) stu[i].PE=stu[i-1].PE; } sort(stu,stu+n,cmp4); for(i=0;i<n;i++) { if(i==0) stu[i].PA=i+1; if(i!=0&&stu[i].A!=stu[i-1].A) stu[i].PA=i+1; if(i!=0&&stu[i].A==stu[i-1].A) stu[i].PA=stu[i-1].PA; } for(i=0;i<n;i++) { stu[i].con=min(min(stu[i].PC,stu[i].PM),min(stu[i].PE,stu[i].PA));//得到最高排名 //得到最高排名的科目名稱(相同的按優先級較高的為準) if(stu[i].PA==stu[i].con) stu[i].val='A'; else { if(stu[i].PC==stu[i].con) stu[i].val='C'; else { if(stu[i].PM==stu[i].con) stu[i].val='M'; else { if(stu[i].PE==stu[i].con) stu[i].val='E'; } } } } int flag; for(i=0;i<m;i++) { scanf("%d",&x); flag=0; for(j=0;j<n;j++) { if(x==stu[j].ID) { printf("%d %c\n",stu[j].con,stu[j].val); flag=1; } if(flag==1) break; } if(flag==0) printf("N/A\n"); } } return 0; }
最後更新:2017-04-03 05:38:56