PAT 1012. The Best Rank (25)

来源:互联网 发布:淘宝加入购物车看不到 编辑:程序博客网 时间:2024/04/20 05:11

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

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

StudentID  C  M  E  A310101     98 85 88 90310102     70 95 88 84310103     82 87 94 88310104     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

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

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 Input5 6310101 98 85 88310102 70 95 88310103 82 87 94310104 91 91 91310105 85 90 90310101310102310103310104310105999999
Sample Output1 C1 M1 E1 A3 AN/A

The code just like below. wish will help.

#include <iostream>#include <vector>#include <algorithm>#include <string>#include <limits.h>using namespace std;class Student {public:    Student() {    }    Student(string id, int C, int M, int E) {        this->id = id;        this->C = C;        this->M = M;        this->E = E;        A = (C + M + E) * 1.0 / 3;    }    string getId() const {        return id;    }    float getA() const {        return A;    }    int getC() const {        return C;    }    int getM() const {        return M;    }    int getE() const {        return E;    }    friend std::istream& operator>>(std::istream&, Student&);    /* for debug use*/    friend std::ostream& operator << (std::ostream&, Student&);    friend std::pair<int, string> getBestRank(vector<Student> students, string id);private:    string id;    int C;    int M;    int E;    float A;};std::istream& operator>>(std::istream& in, Student& stu) {    in >> stu.id;    in >> stu.C;    in >> stu.M;    in >> stu.E;    stu.A = (stu.C + stu.M + stu.E) * 1.0 / 3;    return in;}std::ostream& operator << (std::ostream& out, Student& stu) {    out << stu.id << " " << stu.C << "," << stu.M << " " << stu.E << "," << stu.A << std::endl;    return out;}std::vector<Student>::const_iterator findItem(vector<Student> const &students, string id) {    return std::find_if(students.begin(), students.end(),                        [&](Student const &item) {return item.getId() == id;});}long getRankA(vector<Student> const &students, string id) {    auto temp = findItem(students, id);    if (temp == students.end()) {        return 0L;    }    temp = std::find_if(students.begin(), students.end(),                        [&](Student const& item) {return temp->getA() == item.getA();});    return temp - students.begin() + 1L;}long getRankC(vector<Student> const &students, string id) {    auto temp = findItem(students, id);    if (temp == students.end()) {        return 0L;    }    temp = std::find_if(students.begin(), students.end(),                        [&](Student const& item) {return temp->getC() == item.getC();});    return temp - students.begin() + 1L;}long getRankM(vector<Student> const &students, string id) {    auto temp = findItem(students, id);    if (temp == students.end()) {        return 0L;    }    temp = std::find_if(students.begin(), students.end(),                        [&](Student const& item) {return temp->getM() == item.getM();});    return temp - students.begin() + 1L;}long getRankE(vector<Student> const &students, string id) {    auto temp = findItem(students, id);    if (temp == students.end()) {        return 0L;    }    temp = std::find_if(students.begin(), students.end(),                        [&](Student const& item) {return temp->getE() == item.getE();});    return temp - students.begin() + 1L;}std::pair<long, string> getBestRank(vector<Student> const& sortA,                                   vector<Student> const& sortC,                                   vector<Student> const& sortM,                                   vector<Student> const& sortE, string id) {    std::pair<long, string> result;    result.first = LONG_MAX;    result.second = "A";    long rank = getRankA(sortA, id);    if (rank == 0L) {        return std::pair<long, string>(0, "N/A");    }    if (result.first > rank) {        result.first = rank;        result.second = "A";    }    rank = getRankC(sortC, id);    if (result.first > rank) {        result.first = rank;        result.second = "C";    }    rank = getRankM(sortM, id);    if (result.first > rank) {        result.first = rank;        result.second = "M";    }    rank = getRankE(sortE, id);    if (result.first > rank) {        result.first = rank;        result.second = "E";    }    return result;}/* for debug use */template<class T>void printVector(std::vector<T> const & vectors) {    std::for_each(vectors.begin(), vectors.end(), [](T item) {std::cout << item << ",";});    std::cout << std::endl;}int main(int argc, const char * argv[]) {    // insert code here...    int N = 0;    int M = 0;    std::cin >> N;    std::cin >> M;    if (0 == M) {        return 0;    }    vector<Student> students;    vector<string> index;    for (int i = 0; i < N; ++i) {        Student stu;        std::cin >> stu;        students.push_back(stu);    }    for (int i = 0; i< M; ++i) {        string  id;        cin >> id;        index.push_back(id);    }    vector<Student> sortA(students.begin(), students.end());    std::sort(sortA.begin(), sortA.end(),              [&](Student const & stu1, Student const & stu2) {return stu1.getA() > stu2.getA();});    vector<Student> sortC(students.begin(), students.end());    std::sort(sortC.begin(), sortC.end(),              [&](Student const & stu1, Student const & stu2) {return stu1.getC() > stu2.getC();});    vector<Student> sortM(students.begin(), students.end());    std::sort(sortM.begin(), sortM.end(),              [&](Student const & stu1, Student const & stu2) {return stu1.getM() > stu2.getM();});    vector<Student> sortE(students.begin(), students.end());    std::sort(sortE.begin(), sortE.end(),              [&](Student const & stu1, Student const & stu2) {return stu1.getE() > stu2.getE();});    std::pair<long, string> result;    if (index.size() > 1) {        for (int i = 0; index.size() > 0 && i < index.size() - 1; ++i) {            result = getBestRank(sortA, sortC, sortM, sortE, index[i]);            if (result.first == 0) {                std::cout << result.second << std::endl;                continue;            }            std::cout << result.first << " " << result.second << std::endl;        }        result = getBestRank(sortA, sortC, sortM, sortE, *(index.end() - 1));        if (result.first == 0) {            std::cout << result.second << std::endl;        } else {            std::cout << result.first << " " << result.second << std::endl;        }        return 0;    }    if (index.size() > 0) {        result = getBestRank(sortA, sortC, sortM, sortE, index[0]);        if (result.first == 0) {            std::cout << result.second << std::endl;        } else {            std::cout << result.first << " " << result.second << std::endl;        }    }    return 0;}
原创粉丝点击