1012 The Best Rank (25)
題目描述:
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 91Then 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.
Input
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.
Output
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".
Sample Input5 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
Sample Output
1 C
1 M
1 E
1 A
3 A
N/A
代碼 #include<stdio.h> #include<algorithm> using namespace std;typedef struct stu { int id; int grade[4]; ? //grade[0]保存了平均數? }stu; int x; //x用于cmp判斷當前比較哪門課的成績? bool cmp(stu a,stu b) { if(a.grade[x] != b.grade[x]) { return a.grade[x]>b.grade[x]; } } int compare1(stu arr[],int n,int Id) { int great=100,min,t,j; ?// great為最好的名次,t表示哪門課取得最好的名次? char y; for(int i = 0; i<4; i++) { x = i; sort(arr,arr+n,cmp); for( j = 0; j<n; j++) { if(arr[j].id == Id) { min = j+1; break; } } if(j == n) { printf("N/A\n"); return 0; } if(great>min) ? ? // 比較哪門課名次比較好? { great = min; t = x; } } if(t == 0) { y = 'A'; } else if( t == 1) { y = 'C'; } else if( t == 2) { y = 'M'; } else if( t == 3) { y = 'E'; } printf("%d %c\n",great,y); return 1; } int main(void) { int n,m,Id; scanf("%d%d",&n,&m); stu arr[n]; for(int i=0; i<n; i++) { scanf("%d%d%d%d",&arr[i].id,&arr[i].grade[1],&arr[i].grade[2],&arr[i].grade[3]); arr[i].grade[0] = (arr[i].grade[1]+arr[i].grade[2]+arr[i].grade[3])/3; } for(int i=0; i<m; i++) { scanf("%d",&Id); compare1(arr,n,Id); } }
總結
以上是生活随笔為你收集整理的1012 The Best Rank (25)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Leetcode-45. 跳跃游戏Ⅱ
- 下一篇: Java中关于自增自减