PAT 1012

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 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.

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 Input
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
Sample Output
1 C 
1 M
1 E
1 A
3 A
N/A

题目不难,就是很烦。注意相同排名的处理。

 

代码

 1 #include <stdio.h>
 2 #include < string.h>
 3 #include <algorithm>
 4  using  namespace std;
 5 
 6 typedef  struct Student{
 7      char ID[ 8];
 8      double C,M,E,A;
 9 }Student;
10  int search_rank( double *, double , int);
11  int search_ID(Student *, char *, int );
12  int main()
13 {
14      double gradeC[ 2000],gradeM[ 2000],gradeE[ 2000],gradeA[ 2000];
15     Student student[ 2000];
16      int N,M,i;
17      while(scanf( " %d%d ",&N,&M) != EOF){
18          for(i= 0;i<N;++i){
19             scanf( " %s%lf%lf%lf ",student[i].ID,&student[i].C,&student[i].M,&student[i].E);
20             student[i].A = (student[i].C + student[i].M + student[i].E) /  3;
21             gradeC[i] = student[i].C;
22             gradeM[i] = student[i].M;
23             gradeE[i] = student[i].E;
24             gradeA[i] = student[i].A;
25         }
26         sort(gradeC,gradeC+N);
27         sort(gradeM,gradeM+N);
28         sort(gradeE,gradeE+N);
29         sort(gradeA,gradeA+N);
30          char searchID[ 8];
31          int rank,minRank;
32          char minRankOrder;
33          int ID_index;
34          for(i= 0;i<M;++i){
35             scanf( " %s ",searchID);
36             ID_index = search_ID(student,searchID,N);
37              if(ID_index <  0){
38                 printf( " N/A\n ");
39                  continue;
40             }
41             minRank = search_rank(gradeA,student[ID_index].A,N);
42             minRankOrder =  ' A ';
43              if(minRank ==  1){
44                 printf( " 1 A\n ");
45                  continue;
46             }
47             rank = search_rank(gradeC,student[ID_index].C,N);
48              if(rank < minRank){
49                 minRank = rank;
50                 minRankOrder =  ' C ';
51             }
52              if(minRank ==  1){
53                 printf( " 1 C\n ");
54                  continue;
55             }
56             rank = search_rank(gradeM,student[ID_index].M,N);
57              if(rank < minRank){
58                 minRank = rank;
59                 minRankOrder =  ' M ';
60             }
61              if(minRank ==  1){
62                 printf( " 1 M\n ");
63                  continue;
64             }
65             rank = search_rank(gradeE,student[ID_index].E,N);
66              if(rank < minRank){
67                 minRank = rank;
68                 minRankOrder =  ' E ';
69             }
70             printf( " %d %c\n ",minRank,minRankOrder);                
71         }
72     }
73      return  0;
74 }
75 
76  int search_rank( double *data, double x, int n)
77 {
78      if(n <=  0)
79          return - 1;
80      int i = n- 1;
81      while(i >=  0 && data[i--] != x);
82      if(data[++i] == x)
83          return n - i;
84      else
85          return - 1;
86 }
87  int search_ID(Student *data, char *ID, int n)
88 {
89      if(n <=  0)
90          return - 1;
91      int i= 0;
92      while(i < n && strcmp(data[i++].ID,ID));
93      if(!strcmp(data[--i].ID,ID))
94          return i;
95      else
96          return - 1;
97 }

 

posted on 2014-02-13 22:02 Boostable 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/boostable/p/pat_1012.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值