PAT Advanced 1012. The Best Rank
1. Problem Description:
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.
2. 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.
3. 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
.
4. 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
5. Sample Output:
1 C
1 M
1 E
1 A
3 A
N/A
6. Performance Limit:
Code Size Limit
16 KB
Time Limit
200 ms
Memory Limit
64 MB
思路:
题目要求找出每个学生的最优排名,这里定义结构体Stu
存储每个学生的相关信息,这里要注意下C++中结构体struct的使用与C中的不同之处。根据输入样例,主要有两点需要注意:
- 平均分的计算是四舍五入后的结果。
- 每一科目可能存在同分的情况,所以存在相同排名的情况。
根据题目描述,如果学生在不同的科目上有相同的最优排名,则按照A
> C
> M
> E
的优先级进行输出。这里按照优先级顺序依次使用qsort()
库函数对学生进行排序并记录各自的最优排名,最后按要求输出。具体来说,定义了指针数组pStu1
用于排序,避免移动结构体本身进而节省了时间,最后查询时直接使用的遍历查找,没想到能满足时间要求。。。如果需要缩短时间的话还需要建立一个查找表。
第一次提交时testpoint2报wrong answer,检查了好几个小时找不出逻辑错误。无奈参考大佬题解:1012. The Best Rank (25)-PAT甲级真题_柳婼的博客-CSDN博客 ,瞬间醍醐灌顶,存储排名时存在逻辑错误,比如正确的排名应该是 1 1 3 4 5
而不是 1 1 2 3 4
,真的好蠢的bug QAQ。这里代码写的比较冗余,简洁的代码可以参见大佬题解。
My Code & Result:
#include <iostream> // standard io
#include <cstdlib> // qsort header
#include <string> // string header
using namespace std;
struct Stu
{
//char id[7];
string id;
int c;
int m;
int e;
int avr;
//double avr;
int bestRank;
char bestCourse;
};
int cmp_c(const void *p1, const void *p2);
int cmp_m(const void *p1, const void *p2);
int cmp_e(const void *p1, const void *p2);
int cmp_avr(const void *p1, const void *p2);
// first submit testpoint 2 wrong answer
int main(void)
{
int totalNum=0, checkNum=0;
int tempRank=0;
string tempId;
int rankCount=0; // this solve testpoint 2!!! The correct rank should be 1 1 3 4 5 instead of 1 1 2 3 4
cin >> totalNum >> checkNum;
if(!totalNum || !checkNum) return 0; // want this fixed testpoint 2, but failed
Stu student[totalNum];
Stu *pStu1[totalNum] = {0};
for(int i=0; i<totalNum; ++i) // input info.
{
cin >> student[i].id >> student[i].c >> student[i].m >> student[i].e;
student[i].avr = (student[i].c+student[i].m+student[i].e)*1.0/3 + 0.5;
//student[i].avr = (student[i].c+student[i].m+student[i].e)*1.0/3;
student[i].bestRank = -1;
pStu1[i] = student + i; // assign the address of student
// output info, test output
// cout << student[i].id << " " << student[i].c << " " << student[i].m << " " << student[i].e << " " << student[i].avr << endl;
}
qsort(pStu1, totalNum, sizeof(Stu *), cmp_avr); // sort by avr
tempRank = 1;
rankCount = 0;
for(int i=0; i<totalNum; ++i)
{
++rankCount;
if(i>0 && pStu1[i]->avr != pStu1[i-1]->avr) // update tempRank
{
// ++tempRank;
tempRank = rankCount;
}
if(pStu1[i]->bestRank == -1 || tempRank < pStu1[i]->bestRank) // update bestRank
{
pStu1[i]->bestRank = tempRank;
pStu1[i]->bestCourse = 'A';
}
}
//void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*))
qsort(pStu1, totalNum, sizeof(Stu *), cmp_c); // sort by c program language
tempRank = 1;
rankCount = 0;
for(int i=0; i<totalNum; ++i)
{
// test sort result
//cout << pStu1[i]->id << " " << pStu1[i]->c << endl;
++rankCount;
if(i>0 && pStu1[i]->c != pStu1[i-1]->c) // update tempRank
{
// ++tempRank;
tempRank = rankCount;
}
if(pStu1[i]->bestRank == -1 || tempRank < pStu1[i]->bestRank) // update bestRank
{
pStu1[i]->bestRank = tempRank;
pStu1[i]->bestCourse = 'C';
}
}
qsort(pStu1, totalNum, sizeof(Stu *), cmp_m); // sort by mathematics
tempRank = 1;
rankCount = 0;
for(int i=0; i<totalNum; ++i)
{
// test sort result
//cout << pStu1[i]->id << " " << pStu1[i]->m << endl;
++rankCount;
if(i>0 && pStu1[i]->m != pStu1[i-1]->m) // update tempRank
{
// ++tempRank;
tempRank = rankCount;
}
if(pStu1[i]->bestRank == -1 || tempRank < pStu1[i]->bestRank) // update bestRank
{
pStu1[i]->bestRank = tempRank;
pStu1[i]->bestCourse = 'M';
}
}
qsort(pStu1, totalNum, sizeof(Stu *), cmp_e); // sort by english
tempRank = 1;
rankCount = 0;
for(int i=0; i<totalNum; ++i)
{
++rankCount;
if(i>0 && pStu1[i]->e != pStu1[i-1]->e) // update tempRank
{
// ++tempRank;
tempRank = rankCount;
}
if(pStu1[i]->bestRank == -1 || tempRank < pStu1[i]->bestRank) // update bestRank
{
pStu1[i]->bestRank = tempRank;
pStu1[i]->bestCourse = 'E';
}
}
for(int i=0; i<checkNum; ++i) // check rank & ouput
{
cin >> tempId;
//cout << tempId << endl;
int j=0;
for(j=0; j<totalNum; ++j)
{
//string temp = student[j].id; // convert char * to string
//if(tempId == temp)
if(tempId == student[j].id)
{
cout << student[j].bestRank << " " << student[j].bestCourse << endl;
break;
}
}
if(j == totalNum) cout << "N/A" << endl;
}
return 0;
}
int cmp_c(const void *p1, const void *p2)
{
// Stu *left = (Stu *)*p1; // here make mistake
// Stu *right = (Stu *)*p2;
Stu *left = *(Stu **)p1;
Stu *right = *(Stu **)p2;
return right->c - left->c;
}
int cmp_m(const void *p1, const void *p2)
{
Stu *left = *(Stu **)p1;
Stu *right = *(Stu **)p2;
return right->m - left->m;
}
int cmp_e(const void *p1, const void *p2)
{
Stu *left = *(Stu **)p1;
Stu *right = *(Stu **)p2;
return right->e - left->e;
}
int cmp_avr(const void *p1, const void *p2)
{
Stu *left = *(Stu **)p1;
Stu *right = *(Stu **)p2;
return right->avr - left->avr;
// if(right->avr > left->avr) return 1;
// else if(right->avr < left->avr) return -1;
// else return 0;
}
Compiler
C++ (g++)
Memory
712 / 65536 KB
Time
25 / 200 ms
标签:PAT,Stu,tempRank,void,int,pStu1,Best,student,1012
From: https://www.cnblogs.com/tacticKing/p/17489104.html