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 Algebra), 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

 题目大意:现已知n个考生的3门分数,平均分可以按照这三门算出来。然后分别对这四个分数从高到低排序,这样对每个考生来说有4个排名。

m个查询,对于每一个学生id,输出当前id学生的最好的排名和它对应的科目,如果名次相同,按照A>C>M>E的顺序输出。如果当前id不存在,输出N/A

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 char out[5]= {'A','C','M','E'};
 5 struct Student
 6 {
 7     int id;
 8     int bestrank;
 9     int score[4];
10     int rank[4];
11 } stu[2000];
12 int Exit[1000000];
13 int flag;  //定义全局变量用于排序
14 int cmp( const void *a, const void *b)
15 {
16     struct Student * c = (struct Student *)a;
17     struct Student * d = (struct Student *)b;
18     return d->score[flag] - c->score[flag];  
19 }
20 int main()
21 {
22     int n,m,min,id;
23     int i,j;
24     scanf("%d%d",&n,&m);
25     for( i=0; i<n; i++)
26     {
27         scanf("%d%d%d%d",&stu[i].id,&stu[i].score[1],&stu[i].score[2],&stu[i].score[3]);
28         stu[i].score[0] =(int)((stu[i].score[1]+stu[i].score[2]+stu[i].score[3])/3.0+0.5);  //求平均数
29         Exit[stu[i].id] = 1;  //如果存在则置1
30     }
31     for( flag=0; flag<=3; flag++)
32     {
33         qsort( stu,n,sizeof(stu[1]),cmp);//降序排序
34         stu[0].rank[flag]=1;  //首位为第一名
35         for( i=1; i<n; i++) //得出全体成员的排名
36         {
37             stu[i].rank[flag]=i+1;
38             if( stu[i].score[flag]==stu[i-1].score[flag]) 
39                 stu[i].rank[flag] =stu[i-1].rank[flag];
40         }
41     }
42     for( i=0; i<n; i++)
43     {
44         stu[i].bestrank=0;
45         min = stu[i].rank[0];
46         for(j=1; j<=3; j++)
47         {
48             if( stu[i].rank[j]<min)  //找出排名最小的,即排名最靠前的
49             {
50                 min = stu[i].rank[j];
51                 stu[i].bestrank = j;
52             }
53         }
54     }
55     for( i=0 ; i<m; i++)
56     {
57         scanf("%d",&id);
58         int temp = Exit[id];
59         if( temp )
60         {
61           for( j=0; j<n; j++)
62           {
63               if( stu[j].id==id)
64                   printf("%d %c\n",stu[j].rank[stu[j].bestrank],out[stu[j].bestrank]);
65           }
66         }
67         else printf("N/A\n");
68     }
69     return 0;
70 }

 

转载于:https://www.cnblogs.com/yuxiaoba/p/8548301.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值