A1012The Best Rank

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 (≤), 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

思路:

• 定义结构体存储每个学生的id,四门成绩,定二维数组rank[10000000][4],一维是学生id,二维是四个存储空间代表四门课程;

• 输入n组数据存入结构体数组,对结构体数组进行四种排序,然后将排名记录在rank二维数组中;注意,排名形式是11345而不是11234

• 输入m个id,然后查询id是否在数组中,如果在则找出该id的最小排名,并且按照优先级输出相应课程名称。

问题:

问题1:编译错误rank不明确

问题2:部分答案错误

解决:

解决1:和系统中的标识符重名,将rank改成Rank

解决2:在比较某id学生的四门排名时,要将四个排名全部遍历到。

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cmath>
 4 using namespace std;
 5 struct Students
 6 {
 7     int id;
 8     int score[4];
 9 }stu[2005];
10 int Rank[10000000][4] = {0};
11 int tag;
12 char course[4] = { 'A','C','M','E' };
13 bool cmp(Students a, Students b) {
14     return a.score[tag] > b.score[tag];
15 }
16 int main() {
17     int n,m;
18     cin >> n>>m;
19     for (int i = 0; i < n; i++) {
20         cin >> stu[i].id >> stu[i].score[1] >> stu[i].score[2] >> stu[i].score[3];
21         stu[i].score[0] = round((stu[i].score[1] + stu[i].score[2] + stu[i].score[3])/3.0);
22     }
23     for (tag = 0; tag < 4; tag++) {
24         sort(stu, stu + n, cmp);
25         Rank[stu[0].id][tag] = 1;
26         for (int i = 1; i < n; i++) {
27             if (stu[i].score[tag] == stu[i - 1].score[tag])
28                 Rank[stu[i].id][tag] = Rank[stu[i - 1].id][tag];//将并列情况修正
29             else
30                 Rank[stu[i].id][tag] = i + 1;        }
31     }
32     int idi;
33     for (int i = 0; i < m; i++) {
34         cin >> idi;
35         if (Rank[idi][0] == 0)
36             cout << "N/A" << endl;
37         else {
38             int minn=0;
39             for (int j = 1; j < 4; j++) {
40                 if (Rank[idi][j] < Rank[idi][minn])
41                     minn = j;
42             }
43             cout << Rank[idi][minn] << " " << course[minn] << endl;
44         }
45             
46     }
47     return 0;
48 }
作者:PennyXia
         
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/PennyXia/p/12304191.html