[PTA] PAT(A) 1012 The Best Rank (25 分)

目录

  • Problem
  • Solution
    • Analysis
    • Code

Problem

portal: 1012 The Best Rank (25 分)

Solution

Analysis

?一名学生有三门科目,以及计算出的一个平均成绩,每一个成绩都会有一个排名,现在想要让你输出某一个同学最高的排名(四种成绩排名的最高),以及对应的科目
?如果给定的同学的四种课程排名的名次信息已经存在,那么就很简单,在里面找到最小的名次,以及对应的科目输出即可。
?可以创建四个数组,数组的每个元素存储某一门科目的成绩以及对应的学生学号,之后对这个数组进行排序,等到排完序后,再根据成绩在数组中的位置,设置对应学生的每一门课程的名次,之后就进入了前一步。
?代码不太好看,没有将四个数组存为数组,导致代码又长又乱。

Code

#include <bits/stdc++.h>
using namespace std;

struct my_rank {
    int C_r, M_r, E_r, A_r;
};

bool my_comp(pair<int, string> p1, pair<int, string> p2) {
    return p1.first > p2.first;
}

void setRank(vector<pair<int, string> > vec, map<string, my_rank> &my_map, int flag) {
    int last_value, last_rank, rank;
    for (int i = 0; i < vec.size(); i++) {
        if (i == 0) {
            last_value = vec[0].first;
            last_rank = 1;
            rank = 1;
        } else if (vec[i].first == last_value) {
            rank = last_rank;
        } else {
            rank = i + 1;
            last_value = vec[i].first;
            last_rank = rank;
        }

        switch (flag) {
            case 1:
                my_map[vec[i].second].C_r = rank;
                break;
            case 2:
                my_map[vec[i].second].M_r = rank;
                break;
            case 3:
                my_map[vec[i].second].E_r = rank;
                break;
            case 4:
                my_map[vec[i].second].A_r = rank;
                break;
        }
    }
}

void print_max(map<string, my_rank> &my_map, string id) {
    if (my_map.count(id) == 0) {
        cout << "N/A" << endl;
        return;
    }
    int front_rank = 999999;
    char ch;
    my_rank mr = my_map[id];

    if (mr.A_r < front_rank) {
        front_rank = mr.A_r;
        ch = 'A';
    }
    if (mr.C_r < front_rank) {
        front_rank = mr.C_r;
        ch = 'C';
    }
    if (mr.M_r < front_rank) {
        front_rank = mr.M_r;
        ch = 'M';
    }
    if (mr.E_r < front_rank) {
        front_rank = mr.E_r;
        ch = 'E';
    }

    cout << front_rank << " " << ch << endl;
}

int main(void) {
    vector<pair<int, string> > C_S, M_S, E_S, A_S;
    map<string, my_rank> my_map;
    int N, M;

    cin >> N >> M;
    int c, m, e, a;
    string id;
    for (int i = 0; i < N; i++) {
        cin >> id >> c >> m >> e;
        a = (c + m +  e) / 3;
        C_S.push_back(make_pair(c, id));
        M_S.push_back(make_pair(m, id));
        E_S.push_back(make_pair(e, id));
        A_S.push_back(make_pair(a, id));
    }
    sort(C_S.begin(), C_S.end(), my_comp);
    sort(M_S.begin(), M_S.end(), my_comp);
    sort(E_S.begin(), E_S.end(), my_comp);
    sort(A_S.begin(), A_S.end(), my_comp);
    setRank(C_S, my_map, 1);
    setRank(M_S, my_map, 2);
    setRank(E_S, my_map, 3);
    setRank(A_S, my_map, 4);

    for (int i = 0; i < M; i++) {
        cin >> id;
        print_max(my_map, id);
    }
}

原文地址:https://www.cnblogs.com/by-sknight/p/11479187.html

时间: 2024-10-18 22:20:40

[PTA] PAT(A) 1012 The Best Rank (25 分)的相关文章

PAT Advanced 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 Algrbra), and E - English. At the mean time, we encourage students by e

1012 The Best Rank (25分) vector与结构体排序

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 Algrbra), and E - English. At the mean time, w

[PTA] PAT(A) 1007 Maximum Subsequence Sum (25 分)

目录 Problem Description Input Output Sample Sample Input Sample Output Solution Analysis Code Problem portal: 1007 Maximum Subsequence Sum (25 分) Description Given a sequence of $K$ integers { $N_{1}?$, $N_{2}?$, $...$, $N_{K}$ }. A continuous subsequ

PAT:1012. The Best Rank (25) AC

#include<stdio.h> #include<algorithm> using namespace std; struct Student { int mID; int grade[4]; //0对应平均A,1对应C,2对应M,3对应E }STU[2010]; char course[4]={'A','C','M','E'}; //所有的存储都对应ACME int Rank[10000000][4]={0}; //每个学号四个成绩对应的排名 int now=0; //排序的

PAT(A) 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 e

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 Algrbra), and E - English. At the mean time, we encourage students by e

[PTA] PAT(A) 1009 Product of Polynomials (25 分)

目录 Problem Description Input Output Sample Sample Input Sample Output Solution Analysis Code Result Problem portal:1009 Product of Polynomials Description Input Output Sample Sample Input Sample Output Solution Analysis Code #include <bits/stdc++.h>

PAT 1012. The Best Rank (25)

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, w

1012. The Best Rank (25)——PAT (Advanced Level) Practise

题目信息: 1012. The Best Rank (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Math