PAT-A 真题 – 1012 The Best Rank

发布于 / PAT-甲级 / 0 条评论

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,英语第9,数学第2,平均分第5,则最有利的排名为数学,第二

如果有重复的,则按照A C M E 的权重来求。

例如计算机第1,英语第1,数学第1,平均分第1(大佬)

则最有利的排名为平均分,第一(膜拜)

题目应该很容易理解,这里不做过多解释。这道题博主将近一年没想出来为什么总有两个测试点出错,直到今天。。。

千万记住考虑并列!!!!!千万记住考虑并列!!!!!千万记住考虑并列!!!!!

并列的排名必须是:1 2 2 4 5 6 7 7 7 10 这样的,不能是1 2 2 3 4 5 6 6 6 7 ,更不能是1 2 3 4 5 6 7 8 9 10!!!

气死我了。。。。。。。啊啊啊啊啊啊啊啊啊

代码不解释了!生气气!!!你们自己看吧!

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <map>
using namespace std;

typedef struct{
  int score[4];  //a, c, m, e;
  int rank[4];  //_a,_c,_m,_e;
  int best_rank, best_item;
  int id;
} node;

int flag;
bool cmp(node a, node b){
  return a.score[flag] > b.score[flag];
}

int main(){
  node stu[2010];
  int icnt, qcnt;
  cin >> icnt >> qcnt;
  for(int i = 0; i < icnt; i++){  //读取数据,计算平均成绩 
    cin >> stu[i].id >> stu[i].score[1] >> stu[i].score[2] >> stu[i].score[3];
    stu[i].score[0] = (stu[i].score[1] + stu[i].score[2] + stu[i].score[3]) / 3;
  }
  for(flag = 0; flag < 4; flag++){  //对成绩单各科执行排序 
    sort(stu, stu + icnt, cmp);
    int last_score = 23333, last_rnk = 23333;
    for(int i = 0; i < icnt; i++){ 
      if(stu[i].score[flag] == last_score){//处理并列 
        stu[i].rank[flag] = last_rnk;  //发生并列,这个人的排名和上一个人的排名一样 
        continue;
      }else{
        stu[i].rank[flag] = i + 1;    //没有并列,排名为i+1 
        last_score = stu[i].score[flag];//记录分数,以便下次处理并列 
        last_rnk = i + 1;
      }
    }
  }
  map<int, int> mp;
  for(int i = 0; i < icnt; i++){
    mp[stu[i].id] = i + 1;  //这里+1的目的是防止map默认值为0,下面判断是否存在时出错
    stu[i].best_item = min_element(stu[i].rank, stu[i].rank + 4) - stu[i].rank;
    stu[i].best_rank = stu[i].rank[stu[i].best_item]; 
  }
  char tb[] = {'A', 'C', 'M', 'E'};  //便于一会直接输出 
  for(int i = 0; i < qcnt; i++){
    int id;
    cin >> id;
    if(mp[id])  //刚刚记录map时+1了,现在给减掉 
      cout << stu[mp[id] - 1].best_rank << ' ' << tb[stu[mp[id] - 1].best_item] << endl;
    else cout << "N/A" << endl;
  }
  return 0;
}

转载原创文章请注明,转载自: 斐斐のBlog » PAT-A 真题 – 1012 The Best Rank
目前还没有评论,快来抢沙发吧~