【每日编程-438期】浙江大学上机题(一)

教育   2025-02-05 10:02   江西  

浙江大学上机题(一)


每日编程中遇到任何疑问、意见、建议请公众号留言或加入每日编程群聊739635399



After each PAT, the PAT Center will announce the ranking of institutions based on their students' performances. Now you are asked to generate the ranklist.

输入格式:

Each input file contains one test case. For each case, the first line gives a positive integer N (), which is the number of testees. Then N lines follow, each gives the information of a testee in the following format:

ID Score School

输出格式:

For each case, first print in a line the total number of institutions. Then output the ranklist of institutions in nondecreasing order of their ranks in the following format:

Rank School TWS Ns

where Rank is the rank (start from 1) of the institution; School is the institution code (all in lower case); ; TWS is the total weighted score which is defined to be the integer part of ScoreB/1.5 + ScoreA + ScoreT*1.5, where ScoreX is the total score of the testees belong to this institution on level X; and Ns is the total number of testees who belong to this institution.

The institutions are ranked according to their TWS. If there is a tie, the institutions are supposed to have the same rank, and they shall be printed in ascending order of Ns. If there is still a tie, they shall be printed in alphabetical order of their codes.

输入样例:

10
A57908 85 Au
B57908 54 LanX
A37487 60 au
T28374 67 CMU
T32486 24 hypu
A66734 92 cmu
B76378 71 AU
A47780 45 lanx
A72809 100 pku
A03274 45 hypu

输出样例:

5
1 cmu 192 2
1 au 192 3
3 pku 100 1
4 hypu 81 2
4 lanx 81 2

解决方法:

(1)算法的基本思想:

给定n行列表,每一行为”编号,成绩,学校”,编号第一位代表该成绩属于”A,B,T”,要求统计信息后,按照优先级输出所有学校的”排名,学校名字,学校总加权分数,学校参考人数”,其中要求学校名字统一为全小写。总加权分数计算公式为:ScoreB/1.5 + ScoreA + ScoreT*1.5,结果取整数部分。优先级依次为:一,总加权分数降序;二,参考人数升序;三,学校名字升序。

 

编程思路:直接按照题意处理,先用map记录对应学校的各项信息,再将数据转存到vector中,按照优先级排序后输出。

(2)代码实现:

#include <iostream>
#include <vector>
#include <unordered_map>
#include <string>
#include <algorithm>
#include <cctype>
using namespace std;
struct School
{

    string name;                                  // 学校名字
    int tws;                                      // 总加权分数
    int ns;                                       // 参考人数
    int A, B, T;                                  // 分别记录三种考试的总成绩
    School() : A(0), B(0), T(0), tws(0), ns(0) {} // 构造函数初始化
};

bool cmp(School a, School b)        // 按照优先级排序
{
     if (a.tws == b.tws && a.ns == b.ns)
        return a.name < b.name;
    else if (a.tws == b.tws)
         return a.ns < b.ns;
    else
        return a.tws > b.tws;
}
int main()
{
    int n;
    string id, school;
    int score;
    vector<School> ans;
    unordered_map<string, School> sch;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> id >> score >> school;

        for (int j = 0; j < school.length(); j++) // 转换成全小写
            school[j] = tolower(school[j]);

        if (id[0] == 'A')
            sch[school].A += score;
        else if (id[0] == 'B')
            sch[school].B += score;
        else if (id[0] == 'T')
            sch[school].T += score;

        sch[school].ns++;
        sch[school].name = school;
    }
    unordered_map<string, School>::iterator it;
    for (it = sch.begin(); it != sch.end(); it++)
    { // 转存到vector中
        ans.push_back(it->second);
    }

    for (int i = 0; i < ans.size(); i++)
    { // 计算出总加权分数
        ans[i].tws = (int)(1.0 * ans[i].A + 1.0 * ans[i].B / 1.5 + 1.0 * ans[i].T * 1.5);
    }
    sort(ans.begin(), ans.end(), cmp); // 按照优先级排序
    cout << ans.size() << endl;

    int rank = 1, prev = ans[0].tws;
    for (int i = 0; i < ans.size(); i++)
    {
        // tws小于前个学校,更新排名,否则排名与前个学校相同
        if (ans[i].tws < prev)
        {
            prev = ans[i].tws;
            rank = i + 1;
        }
        cout << rank << " " << ans[i].name << " " << ans[i].tws << " " << ans[i].ns << endl;
    }
    return 0;
}

明日预告:浙江大学上机题(二)

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:

  • The left subtree of a node contains only nodes with keys less than the node's key.

  • The right subtree of a node contains only nodes with keys greater than or equal to the node's key.

  • Both the left and right subtrees must also be binary search trees.

If we swap the left and right subtrees of every node, then the resulting tree is called the Mirror Image of a BST.

Now given a sequence of integer keys, you are supposed to tell if it is the preorder traversal sequence of a BST or the mirror image of a BST.

输入格式:

Each input file contains one test case. For each case, the first line contains a positive integer  (). Then  integer keys are given in the next line. All the numbers in a line are separated by a space.

输出格式:

For each test case, first print in a line YES if the sequence is the preorder traversal sequence of a BST or the mirror image of a BST, or NO if not. Then if the answer is YES, print in the next line the postorder traversal sequence of that tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.

输入样例:

7
8 6 5 7 10 8 11
7
8 10 11 8 6 7 5
7
8 6 8 5 10 9 11

输出样例:

YES
5 7 6 8 11 10 8
YES
11 8 10 7 5 6 8
NO


灰灰考研
最全的【计算机考研】【软件考研】考研信息! 最丰富的共享资料! 最大程度上帮助学渣狗登上研究生大门!
 最新文章