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 CME 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 Specification:

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 CM and E. Then there are M lines, each containing a student ID.

Output Specification:

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 

题目大意:

给出学生4门课的成绩,让求每一位学生排名最靠前的课程和排名,如果不存在这个学生就返回N/A。

思路:

这道题的思路其实不难,但当时没有采用一种很好很简便的方法来写这道题,参考了网上的代码以后,重写了代码。
新的思路:
1.为学生结构体设置排行和分数数组。
2.设立exist数组,记录学生是否存在(是否非0),如果非0,则记录结构体下标。
3.先按分数排序,排序完了以后重新更正排名(记录并列的情况),然后遍历结构体数组,挑选出最好的排名。

 1 #include<iostream>
 2 #include<algorithm>
 3 using namespace std;
 4 int flag=0;
 5 int exist[1000000];
 6 struct stu
 7 {
 8     int id,best;   // 六位的学号,最好的排名,最好排名的序号
 9     int score[4],rank[4];  // 四门课的分数,注意这道题的平均分是取整的
10 }s[2005];
11 bool cmp(stu a,stu b)    //这样的写法可以方便排序
12 {
13     return a.score[flag]>b.score[flag];
14 }
15 char kw[] ={'A','C','M','E'};
16 int main()
17 {
18     int n,m,id;
19     scanf("%d %d",&n,&m);
20     for(int i=0;i<n;i++)
21     {
22         scanf("%d %d %d %d",&s[i].id,&s[i].score[1],&s[i].score[2],&s[i].score[3]);
23         s[i].score[0] = (s[i].score[1]+s[i].score[2]+s[i].score[3])/3.0+0.5;  // 计算平均分,四舍五入
24         s[i].best = 10000;
25     }
26     for(flag = 0;flag<4;flag++)
27     {
28         sort(s,s+n,cmp);
29         s[0].rank[flag]=1;
30         for(int i=1;i<n;i++)
31         {
32             s[i].rank[flag]=i+1;
33             if(s[i-1].score[flag]==s[i].score[flag])
34             {
35                 s[i].rank[flag]=s[i-1].rank[flag];
36             }
37         }
38 
39     }
40     for(int i=0;i<n;i++)
41     {
42         exist[s[i].id] = i+1;
43         s[i].best = 0;
44         int minn = s[i].rank[0];
45         for(int j=1;j<4;j++)
46         {
47             if(minn>s[i].rank[j])
48             {
49                 minn = s[i].rank[j];
50                 s[i].best = j;
51             }
52         }
53     }
54     for(int i=0;i<m;i++)  // 查找最佳排名
55     {
56         scanf("%d",&id);
57         if(exist[id])
58         {
59             int temp = s[exist[id]-1].best;
60             printf("%d %c\n",s[exist[id]-1].rank[temp],kw[temp]);
61         }
62         else
63         {
64             printf("N/A\n");
65         }
66     }
67     return 0;
68 }