1. 程式人生 > >PTA (Advanced Level)1012 The Best Rank

PTA (Advanced Level)1012 The Best Rank

The 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 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

解題思路:
  本題給出學生數量n,與查詢數量m,之後n行為學生資訊,包括id,C語言得分, 數學得分,英語得分,按照平均分、C語言、數學、英語分別排序(專案名分別為A, C, M, E),之後m行查詢為學生id,要求輸出查詢學生的這四個排名中的最高排名,若出現某幾項排名相同且都該學生最高排名,輸出優先順序平均分 > C語言 > 數學 > 英語。

  定義一個結構體student記錄每個學生的資訊

struct student{
    int id; //記錄學號
    int score[4];   //score[0]為平均分 score[1]C語言 score[2]數學 score[3]英語 
    //為了方便輸出直接按優先順序順序記錄學生分數
};

  用一個vector容器儲存所有學生資訊,一個int型的二維陣列rank_stu[ i ][ j ]記錄排名資訊,i為學生排名, j為排名依照項,同樣 0 為平均分 1 C語言  2 數學  3 英語 。之後對所有專案進行排名,將得到的排名記錄入rank_stu中就可以開始查找了。

  查詢時要先判斷輸入的查詢學號存不存在,可以將rank_stu初始化為0,如果查詢當前學號某一排名為 0 證明該學號不存在,若存在,比較其所有專案排名並輸出最高項排名與專案名。

  AC程式碼

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int inf = INT_MAX;
 4 //無窮大
 5 const int maxn = 1000000;
 6 //學號最大值
 7 const char course[4] = {'A', 'C', 'M', 'E'};
 8 //記錄專案名
 9 struct student{
10     int id; //記錄學號
11     int score[4];   //score[0]為平均分 score[1]C語言 score[2]數學 score[3]英語
12     //為了方便輸出直接按優先順序順序記錄學生分數
13 };
14 vector<student> stu;    //記錄學生資訊
15 int rank_stu[maxn][4];  //記錄排名
16 int courseNow;  //當前正在排名的課程
17 int n, m;   //學生數量,查詢數量
18 bool cmp(student a, student b){
19     return a.score[courseNow] > b.score[courseNow];
20 }
21 //排序依照當前courseNow的對應成績
22 void getRank(){
23     memset(rank_stu, 0, sizeof(rank_stu));
24     //初始化rank_stu為0
25     for(int i = 0; i < 4; i++){ //獲得並記錄四個專案的排名
26         courseNow = i;  //設定當前排名課程
27         sort(stu.begin(), stu.end(), cmp);  //對stu進行排序
28         int cnt = 0;
29         vector<student>::iterator preit = stu.begin();  //preit記錄獲取排名過程中上一個位置迭代器
30         for(vector<student>::iterator it = stu.begin(); it != stu.end(); it++){
31             cnt++;  //記錄當前學生位置
32             if(it == stu.begin()){  //若it排名第一直接記錄排名為cnt(cnt當前值為1)
33                 rank_stu[it->id][courseNow] = cnt;
34                 preit = it; //記錄下一個學生的前一個迭代器為it
35             }else{
36                 if(preit->score[courseNow] == it->score[courseNow]){
37                     //前一個學生與本學生成績相同則排名相同
38                     rank_stu[it->id][courseNow] = rank_stu[preit->id][courseNow];
39                 }else{
40                     rank_stu[it->id][courseNow] =  cnt;
41                     //否則記錄當前排名為cnt
42                 }
43                 preit = it;//記錄下一個學生的前一個迭代器為it
44             }
45         }
46     }
47 }
48 int main(){
49     while(scanf("%d%d", &n, &m) != EOF){    //輸入學生數量與排名數量
50         while(n--){
51             student temp;
52             scanf("%d%d%d%d", &temp.id, &temp.score[1], &temp.score[2], &temp.score[3]);
53             //輸入學生資訊
54             temp.score[0] = (temp.score[1] + temp.score[2] + temp.score[3]) / 3;
55             //計算平均分
56             stu.push_back(temp);
57         }
58 
59         getRank();
60         //獲取排名
61         while(m--){
62             int query;
63             scanf("%d", &query);
64             //輸入查詢id
65             if(rank_stu[query][0] == 0){    //學生不存在
66                 printf("N/A\n");
67                 continue;
68             }
69             int hRank = inf;    //初始化最高排名為無窮大
70             int hRcourse = -1;  //hRcourse記錄最高排名對應的專案
71             for(int i = 0; i < 4; i++){
72                 if(rank_stu[query][i] < hRank){
73                     hRank = rank_stu[query][i];
74                     hRcourse = i;
75                 }
76             }
77             printf("%d %c\n", hRank, course[hRcourse]);
78             //輸出排名與專案名
79         }
80     }
81     return 0;
82 }