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 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 C, M 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
自己的代码
没能ac,因为成绩排名没有考虑到相等成绩相同排名的情况,导致各种乱改乱修,最后直接改到心态崩溃。
总结
- 一开始的思路和选择很重要,对于如此菜的我,前期应该多注重思考设计,不要边想边做。
- 当理想与现实有差距时, 正是锻炼心态的最佳时机。
2.1 做题时注重解决问题的过程,时间花的越多越能锻炼自己保持平和心态的能力。
2.2 认真思考出现的问题本质,不允许自欺欺人。
2.3 ac固然爽,然而时间有限,重点是知识点和自己欠缺的能力。
2.4 遵循先看知识点后做题的套路,相信所有题目都能做,敢于面对崩溃,并在崩溃时记录抉择的过程
#include<iostream>
#include<fstream>
#include<algorithm>
#include<string.h>
using namespace std;
typedef struct stu stu;
struct stu{
char id[11];
int Cprogram;
int Math;
int English;
int Average;
}students[2001], A[2001], M[2001], C[2001], E[2001];
int cmp1(stu a, stu b) { return a.Cprogram > b.Cprogram; }
int cmp2(stu a, stu b) { return a.Math > b.Math; }
int cmp3(stu a, stu b) { return a.English > b.English; }
int cmp4(stu a, stu b) { return a.Average > b.Average; }
int main() {
#ifdef _DEBUG
ifstream cin("data.txt");
#endif
int SumNum, Checknum, c;
int i;
cin >> SumNum >> Checknum;
for(i = 0; i < SumNum; i++) {
cin >> students[i].id >> students[i].Cprogram >> students[i].Math >> students[i].English ;
students[i].Average = (students[i].Math + students[i].Cprogram + students[i].English) / 3 ;
A[i] = M[i] = C[i] = E[i] = students [i];
}
sort(A, A + SumNum, cmp4);
sort(M, M + SumNum, cmp2);
sort(E, E + SumNum, cmp3);
sort(C, C + SumNum, cmp1);
char temp[11];
for(i = 0; i < Checknum; i++) {
cin >> temp;
for(int j = 0; j < SumNum; j++) {
if(strcmp(A[j].id, temp) == 0) {printf("%d A\n",j + 1 ); break;}
else if(strcmp(C[j].id, temp) == 0) {printf("%d C\n",j + 1 ); break;}
else if(strcmp(M[j].id, temp) == 0) {printf("%d M\n",j + 1 ); break;}
else if(strcmp(E[j].id, temp) == 0) {printf("%d E\n",j + 1 ); break;}
else if(j == SumNum - 1) printf("N/A\n");
}
}
#ifdef _DEBUG
cin.close();
#endif
return 0;
}
改进
发现用之前的思路很难对比单科排名相同的学生,故改进,用rank [10000000] [4] 存储排名信息,并且用id可以直接搜到学生(此处犯了个错误,没有把rank设为全局变量),最后比较rank [temp] [0-4] 从零到3找出最小的即可
AC代码
#include<iostream>
#include<fstream>
#include<algorithm>
#include<string.h>
using namespace std;
typedef struct stu stu;
struct stu {
int id;
int socres[4]; //A C M E
}students[10000];
int sequence = 0;
int Rank[10000000][4] = {0};
int cmp1 (stu a, stu b) {
return a.socres[sequence] > b.socres[sequence] ;
}
int main() {
#ifdef _DEBUG
ifstream cin("data.txt");
#endif
char note[4] = {'A','C','M','E'};
int SumNum, CheckNum;
int i,j;
cin >> SumNum >> CheckNum;
for(i = 0; i < SumNum; i++) {
cin >> students[i].id >> students[i].socres[1] >>students[i].socres[2] >> students[i].socres[3];
students[i].socres[0] = students[i].socres[1] +students[i].socres[2] + students[i].socres[3];
}
for(i = 0; i < 4; i++) {
sort(students, students + SumNum, cmp1);
Rank[students[0].id][i] = 1;
for(j = 1; j < SumNum; j++) {
if(students[j].socres[i] == students[j - 1].socres[i])
Rank[students[j].id][i] = Rank[students[j - 1].id][i];
else
Rank[students[j].id][i] = j + 1;
}
sequence++;
}
int temp;
for(i = 0; i < CheckNum; i++) {
cin >> temp;
if(Rank[temp][0] == 0){ printf("N/A\n") ; continue;}
int k = 0;
for(j = 0; j < 4; j++) {
if(Rank[temp][j] < Rank[temp][k]) k = j;
}
printf("%d %c\n",Rank[temp][k], note[k]);
}
#ifdef _DEBUG
cin.close();
#endif
return 0;
}