[code] PTA胡凡算法笔记 DAY020

本文解析了PAT竞赛中的三道排序题目,包括按条件排序学生信息、筛选百万富翁及PAT成绩统计。详细介绍了C++代码实现,重点在于比较函数(cmp)的设计与应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目A1028 List Sorting

在这里插入图片描述

  • 题意
    按输入C的数值决定排序方式,排序后输出。
    C = 1 按ID升序
    C = 2 按name从小到大,相同按ID升序。
    C = 3 按grade从小到大,相同按ID升序

  • 思路
    利用结构体存储数据,然后实现三个排序的cmp函数,按对应C值调用后输出即可。

  • Code in C++

#include <iostream>
#include <string>
#include <algorithm>

#define maxn 100001

struct student {
    std::string ID;
    std::string name;
    int grade;

    void print() {
        std::cout << ID << " " << name << " " << grade << std::endl;
    }
}stu[maxn];

bool cmp1(student a, student b) {
    return a.ID < b.ID;
}

bool cmp2(student a, student b) {
    if (a.name != b.name) return a.name <= b.name;
    else return a.ID < b.ID;
}

bool cmp3(student a, student b) {
    if (a.grade != b.grade) return a.grade <= b.grade;
    else return a.ID < b.ID;
}


int main()
{
    int N, C;
    std::cin >> N >> C;
    for (int i = 0; i < N; ++i) {
        std::cin >> stu[i].ID >> stu[i].name >> stu[i].grade;
    }
    // 根据C排序
    switch (C) {
    case 1:
        std::sort(stu, stu + N, cmp1);
        break;
    case 2:
        std::sort(stu, stu + N, cmp2);
        break;
    case 3:
        std::sort(stu, stu + N, cmp3);
    }

    for (int i = 0; i < N; ++i) {
        stu[i].print();
    }

    return 0;
}


题目A1055 The World’s Richest

在这里插入图片描述

  • 题意
    net_worth排序后,根据年龄范围输出限定个数百万富翁的信息。

  • 思路
    因为这里M<=100,所以这里我就没有再限制最后输出的范围应该是年龄范围内,100名。这里关于年龄范围限定的话,我采用的就是简单的验证,满足则输出,否则继续遍历的策略。

  • Code in C++

#include <iostream>
#include <string>
#include <algorithm>

#define maxn 100001
struct billionaire {
    std::string name;
    int age;
    int net_worth;

    void print() {
        std::cout << name << " " << age << " " << net_worth << std::endl;
    }
}bire[maxn];

bool cmp (billionaire a, billionaire b) {
    if (a.net_worth != b.net_worth) return a.net_worth > b.net_worth;
    else if (a.age != b.age) return a.age < b.age;
    else if (a.name != b.name) return a.name < b.name;
}

bool isValid(int age, int amin, int amax) {
    if (age >= amin && age <= amax) return true;
    else return false;
}

int main()
{
    int N, M;
    std::cin >> N >> M;
    for (int i = 0; i < N; ++i) {
        std::cin >> bire[i].name >> bire[i].age >> bire[i].net_worth;
    }
    std::sort(bire, bire + N, cmp);
    for (int i = 1; i <= M; ++i) {
        int num, amin, amax;
        std::cin >> num >> amin >> amax;
        std::cout << "Case #" << i << ":" << std::endl;
        int count = 0;

        for (int j = 0; j < N; ++j) {
            if (count >= num) break;
            if (isValid(bire[j].age, amin, amax)) {
                bire[j].print();
                ++count;
            }
        }
        if (count == 0) std::cout << "None" << std::endl;
    }
    return 0;
}


题目A1075 PAT Judge

在这里插入图片描述

  • 题意
    给定PAT每题的满分。然后输入用户记录。编译没有通过的表示为-1,但是其最后该题的得分为0。没有提交的题目得分显示为-。根据总分数,解题数目降序以及ID升序排序输出。

  • 思路
    id可以直接和数组的下标做映射,初始化时每题分数都为-1。然后判断每次输入的分数,当输入-1时,将分数改为0便于输出。其他就是按规定写好cmp函数。
    注意:注意这里结构体中关于分数数组的初始化。[😢调了1h bug]

  • Code in C++

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>

#define maxn 10001
struct pat {
    int ID;
    int partial_score[5];
    int total;
    int perfect;
    bool isPrint;
} pats[maxn];
int N, K, M;
int full_mark[5];

bool cmp(pat a, pat b) {
    if (a.total != b.total) return a.total > b.total;
    else if (a.perfect != b.perfect) return a.perfect > b.perfect;
    else return a.ID < b.ID;
}

void init() {
    for (int i = 1; i <= N; ++i) {
        pats[i].ID = i;
        pats[i].total = 0;
        pats[i].perfect = 0;
        pats[i].isPrint = false;
        memset(pats[i].partial_score, -1, sizeof(pats[i].partial_score));
    }
}

int main()
{
    std::cin >> N >> K >> M;
    init();
    for (int i = 0; i < K; ++i) std::cin >> full_mark[i];

    for (int i = 0; i < M; ++i) {
        int tmp, id, score;
        std::cin >> id >> tmp;
        pats[id].ID = id;
        std::cin >> score;
        if (score != -1)  {
            pats[id].isPrint = true;
        }
        // 改为0,便于后续输出
        if (score == -1 && pats[id].partial_score[tmp - 1] == -1) pats[id].partial_score[tmp - 1] = 0;
        if (score > pats[id].partial_score[tmp - 1]) {
            pats[id].partial_score[tmp - 1] = score;
            if (score == full_mark[tmp - 1]) ++pats[id].perfect;
        }

    }


    for (int i = 1; i <= N; ++i) {
        for (int j = 0; j < K; ++j) {
            if (pats[i].partial_score[j] != -1)
                pats[i].total += pats[i].partial_score[j];
        }
    }

    std::sort(pats + 1, pats + N + 1, cmp);
    int r = 1;
    for (int i = 1; i <= N && pats[i].isPrint; ++i) {
        if (i > 0 && pats[i].total != pats[i - 1].total) {
            r = i;
        }
        printf("%d %05d %d", r, pats[i].ID, pats[i].total);
        for (int j = 0; j < K; ++j) {
            if (pats[i].partial_score[j] == -1) printf(" -");
            else printf(" %d", pats[i].partial_score[j]);
        }
        printf("\n");
    }
    return 0;
}


小结

今天做的题都没什么难度,主要就是cmp函数的写法。然后再就是注意一下结构体的初始化问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值