1012 The Best Rank (25)(25 分)

1012 The Best Rank (25)(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 Algebra), 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.node

For example, The grades of C, M, E and A - Average of 4 students are given as the following:ios

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.git

Inputide

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.測試

Outputlua

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.spa

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.code

If a student is not on the grading list, simply output "N/A".orm

Sample Inputblog

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



錯了第三個數據,晚上再改
中午忽然想起來一個問題,會出現並列第一, 第 i 的狀況沒有處理
此外 是否須要 四捨五入取 平均數? 測試提交了一下:發現不四捨五入(手動)也是能夠的

1 C 1 M 1 E 1 A 3 A N/A
//重複代碼不少, 晚上修改一下
#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>
using namespace std ; 

int n , m ; 
#define maxn 3000

struct node{
    string ID ; 
    int C,
        M,
        E,
        A;
    int rank_C,
        rank_M,
        rank_E,
        rank_A;
}; 

node peoper[maxn] ; 

bool cmp_C(node a , node b){
    return a.C > b.C ; 
}

bool cmp_M(node a , node b){
    return a.M > b.M ; 
}

bool cmp_E(node a , node b){
    return a.E > b.E ; 
}

bool cmp_A(node a , node b){
    return a.A > b.A ; 
}

char check_max_cat(int i, int max_rank){
    
    if(peoper[i].rank_A == max_rank){
        return 'A' ; 
    }else if(peoper[i].rank_C == max_rank){
        return 'C' ; 
    }else if(peoper[i].rank_M == max_rank){
        return 'M' ; 
    }else if(peoper[i].rank_E == max_rank){
        return 'E' ; 
    }

}

int check_rank(int i){
    int rank = min(peoper[i].rank_C, min(peoper[i].rank_M, min(peoper[i].rank_E, peoper[i].rank_A)));

    return rank ; 
}

bool check(string ID , int &rank, char &category){
    for(int i=0 ; i<n ; i++){
        if(peoper[i].ID == ID){
            rank = check_rank(i) ; 
            category = check_max_cat(i, rank) ; 
            return true ; 
        }
    }
    return false ; 
}

int main(){

    while(cin >> n >> m ){
        // input 
        for(int i=0 ; i<n ; i++){
            cin >> peoper[i].ID >> peoper[i].C >> peoper[i].M >> peoper[i].E  ;
            //peoper[i].A = (peoper[i].C + peoper[i].M + peoper[i].E)/3+0.5 ;        
            peoper[i].A = (peoper[i].C + peoper[i].M + peoper[i].E) /3;

            peoper[i].rank_C = peoper[i].rank_M = peoper[i].rank_E = peoper[i].rank_A = 0 ; 
        }

        sort(peoper , peoper+n , cmp_C) ; 

        for(int i=0 ; i<n ; i++){
            peoper[i].rank_C = i+1 ; 
            // 可能出現 並列名次 的狀況
            if(i>0 && peoper[i].C == peoper[i-1].C){
                peoper[i].rank_C = peoper[i-1].rank_C ; 
            }
        }

        sort(peoper, peoper + n , cmp_M) ; 
        for(int i=0 ; i<n ; i++){
            peoper[i].rank_M = i + 1 ; 
            if(i>0 && peoper[i].M == peoper[i-1].M){
                peoper[i].rank_M = peoper[i-1].rank_M ; 
            }
        }

        sort(peoper, peoper + n , cmp_E) ; 
        for(int i=0 ; i<n ; i++){
            peoper[i].rank_E = i + 1 ;
            if(i>0 && peoper[i].E == peoper[i-1].E){
                peoper[i].rank_E = peoper[i-1].rank_E ; 
            } 
        }

        sort(peoper, peoper + n , cmp_A) ; 
        for(int i=0 ; i<n ; i++){
            peoper[i].rank_A = i + 1 ; 
            if(i>0 && peoper[i].A == peoper[i-1].A){
                peoper[i].rank_A = peoper[i-1].rank_A ; 
            }
        }

        int rank ;
        char category ; 
        string ID ; 

        for(int i=0 ; i<m ; i++){
            cin >> ID ; 
            if(check(ID,rank, category)){
                cout << rank << " " << category << endl ; 
            }else {
                cout << "N/A" << endl ; 
            }
        }
    }

    return 0 ; 
}    
相關文章
相關標籤/搜索