PAT Level A 1137 Final Score

Link to original title

For students who study "Data Structure" courses in Chinese university MOOCs, in order to obtain a qualified certificate, they must first obtain no less than 200 points for online programming assignments, and then obtain no less than 60 points (out of 100) for the overall assessment.

The calculation formula of the overall evaluation score is G=(Gmid−term×40%+Gfinal×60%), if Gmid−term>Gfinal; otherwise, the overall evaluation G is Gfinal.

Here Gmid−term and Gfinal are students' midterm and final grades respectively.

The problem now is that each test produces a separate score card.

For this question, please write a program to combine different transcripts into one.

Input Format
Input 3 integers are given on the first line, which are P (number of students who have done online programming assignments), M (number of students who have taken midterm exams), and N (number of students who have taken final exams).

Next there are three blocks of input.

The first block contains P online programming grades Gp;

The second block contains M midterm exam grades Gmid−term;

The third block contains N final exam grades Gfinal.

Each grade occupies one line, and the format is: student number score.

Among them, the student ID is English letters and numbers with no more than 20 characters; the score is a non-negative integer (the maximum total score for programming is 900 points, and the maximum score for midterm and final is 100 points).

Output Format
Print out a list of students who received a certificate of eligibility.

One line per student, in the format:

Student number Gp Gmid−term Gfinal G
If some grades do not exist (for example, someone did not take the midterm exam), output "−1" in the corresponding position.

The output order is descending according to the total score (rounded to an integer).

If there is a tie, it will be incremented according to the student number.

The topic guarantees that the student ID is not repeated, and there is at least one qualified student.

Data range
1≤P,M,N≤10000
Input example:
6 6 7
01234 880
a1903 199
ydjh2 200
wehu8 300
dx86w 220
missing 400
ydhfu77 99
wehu8 55
ydjh2 98
dx86w 88 a1903 86 01234 39 ydhfu77 88
a1903 66 01234 58 wehu8 84 ydjh2 82 missing 99 dx86w 81 output example: missing 400 -1 99 99 ydjh2 200 98 82 88 dx86w 220 88 81 84 wehu8 300 55 84 84












My solution:

#include <bits/stdc++.h>
using namespace std;
struct Student{
    string id;
    int p, m, f, s;
    Student(): p(-1), m(-1), f(-1), s(0){}
    
    void calc(){
        if(m > f) s = round(m*0.4 + f*0.6);
        else s = f;
    }
    
    bool operator< (const Student& t) const{
        if(s != t.s) return s > t.s;
        else{
            return id < t.id;
        }
    }
};

int main(){
    int p, m, n;
    cin >> p >> m >> n;
    
    unordered_map<string, Student> hash;
    
    string id;
    int s;
    
    for(int i = 0; i < p; i ++ ){
        cin >> id >> s;
        hash[id].id = id;
        hash[id].p = s;
    }
    
    for(int i = 0; i < m; i ++ ){
        cin >> id >> s;
        hash[id].id = id;
        hash[id].m = s;
    }
    
    for(int i = 0; i < n; i ++ ){
        cin >> id >> s;
        hash[id].id = id;
        hash[id].f = s;
    }
    vector <Student> students;
    for(auto per : hash){
        auto stu = per.second;
        stu.calc();
        if(stu.p >= 200 && stu.s >= 60) students.push_back(stu);
    }
    sort(students.begin(), students.end());
    for(auto re : students){
        cout << re.id << ' ' << re.p << ' ' << re.m << ' ' << re.f << ' ' << re.s << endl;
    }
    return 0;
}

reward:

  Structure constructor initialization: Student(): p(-1), m(-1), f(-1), s(0){}

Define the internal function of the structure

Guess you like

Origin blog.csdn.net/weixin_45660485/article/details/126073059