PAT 1028 List Sorting (25分) 用char[],不要用string

树莓派部署Home Assistant:打造本地化智能家居大脑 家庭自动化旨在通过技术提升居住便利性与舒适度,其核心在于中枢控制系统对各类设备的统一调度。传统云端方案虽部署简便,但存在隐私泄露、网络依赖与生态壁垒等固有缺陷。本地化部署通过将控制逻辑与数据存储于家庭内部网络,从根本上解决了这些问题,实现了数据自主、离线可用与跨品牌联动。Home Assistant作为领先的开源家庭自动化平台,结合树莓派这一低功耗、高性价比的硬件载体,为构建私有、可靠的智能家居中枢提供了成熟方案。本文以树莓派4B为例,详细阐述了从系统安装、设备集成到自动化场景创建的完整实践流程,并深入探讨 阅读详情

题目

Excel can sort records according to any column. Now you are supposed to imitate this function.

Input Specification:
Each input file contains one test case. For each case, the first line contains two integers N (≤10​5 ) and C, where N is the number of records and C is the column that you are supposed to sort the records with. Then N lines follow, each contains a record of a student. A student’s record consists of his or her distinct ID (a 6-digit number), name (a string with no more than 8 characters without space), and grade (an integer between 0 and 100, inclusive).

Output Specification:
For each test case, output the sorting result in N lines. That is, if C = 1 then the records must be sorted in increasing order according to ID’s; if C = 2 then the records must be sorted in non-decreasing order according to names; and if C = 3 then the records must be sorted in non-decreasing order according to grades. If there are several students who have the same name or grade, they must be sorted according to their ID’s in increasing order.

Sample Input 1:
3 1
000007 James 85
000010 Amy 90
000001 Zoe 60
Sample Output 1:
000001 Zoe 60
000007 James 85
000010 Amy 90
Sample Input 2:
4 2
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 98
Sample Output 2:
000010 Amy 90
000002 James 98
000007 James 85
000001 Zoe 60
Sample Input 3:
4 3
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 90
Sample Output 3:
000001 Zoe 60
000007 James 85
000002 James 90
000010 Amy 90

题目解读

给出N个学生信息和一个数字C,每个学生的信息包括ID(6位数字),姓名(长度最多为8的字符串,无空格),分数(0-100)。根据数字C的取值,对学生信息按照不同策略进行排序,最终输出排序后的学生信息:

C=1:按 ID 递增;
C=2:按姓名递增,如果同名,按ID递增;
C=3:按分数递增,如果同分,按ID递增。

思路分析

这不就是三种排序策略就完了吗?

  • 首先创建结构体 Student 保存学生信息,注意 name 字段不要用string!!!,否则你最后一个测试点会是 运行超时,实际是内存溢出!
    在这里插入图片描述
    其实可以想想,题目给出说明姓名字段长度不超过10个字符,怎么可能没用呢,是吧!
  • 如何排序,因为我们使用sort()函数对整个结构体数组进行排序,自己实现的比较函数只能是这样int cmp(Student a, Student b),所以我们要把 C 定义成一个全局变量,然后在这个函数中根据 C的取值进行不同的逻辑实现,当然你也可以实现三个比较函数,但我觉得没有必要。

代码

题目比较简单,代码注释也挺详细,没什么好说的。有个地方需要注意:学号是6位数字,输出必须用printf("%06d", id)格式化。

#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;

// 学生信息
struct Student {
    int id, score;
    // string name;
    char name[10];
}stu[10001];

// 根据那个字段排序
int flag;

// 自定义比较函数
int cmp(Student a, Student b) {
    // 按照ID递增
    if (flag == 1) return a.id < b.id;
    // 按照姓名自增
    else if (flag == 2) {
        // 重名就比较ID
        if (strcmp(a.name, b.name) == 0) return a.id < b.id;
        return strcmp(a.name, b.name) <= 0;
    } else {
        // 按照分数递增,相同就比较ID
        return a.score != b.score ? a.score < b.score : a.id < b.id;
    }
}

int main() {

    // N 个学生
    int n;
    // 根据哪个字段排序
    cin >> n >> flag;
    // 读入学生信息
    for (int i = 0; i < n; ++i) {
        // cin >> stu[i].id >> stu[i].name >> stu[i].score;
        scanf("%d %s %d", &stu[i].id, stu[i].name, &stu[i].score);
    }
    // 排序
    sort(stu, stu + n, cmp);
    // 输出
    for (int i = 0; i < n; ++i) {
        // printf("%06d ", stu[i].id);
        // cout << stu[i].name << " " << stu[i].score << endl;
        printf("%06d %s %d\n", stu[i].id, stu[i].name, stu[i].score);
    }

    return 0;
}

【智能制造】- APS系列|05 APS 排产的五大底层算法逻辑 很多人误以为APS只是给生产工单排先后顺序,实则整套系统依托成熟算法体系,完成多维度约束下的最优生产决策在制造业数字化排产普及的当下,依旧有不少制造管理者对APS存在认知误区,将其等同于简易工单排序工具。事实上,专业级高级计划与排程系统,早已脱离单纯按时间排序的基础功能,而是依托一整套成熟完善的底层算法逻辑,整合现场各类生产限制条件,在海量变量中自动推演、筛选、输出最贴合企业经营目标的生产方案。 阅读详情

相关推荐

智能体评测技术与实践:从评估维度到DeepEval实战指南

智能体评测技术与实践:从评估维度到DeepEval实战指南 本文系统探讨了AI智能体的评测体系与实践方法。首先从四个关键维度构建评估框架:基础能力(意图理解、知识完整性)、复杂任务(多轮对话、动态工作流)、可靠性(幻觉抑制、安全合规)以及工具效率(参数填充、多工具协同)。其次分析了主流评测工具,包括xBench的动态双轨评估、DeepEval的全链路监控等解决方案。最后详细介绍了DeepEval的实战应用,涵盖环境配置、测试用例构建、指标配置及CI/CD集成,展示了如何通过代码化评估实现智能体质量的持续提升

Linda的自动化测试之旅 3416

PAT乙级——1085(多标准排序)java实现

没有AC,19分,暂时没找到问题所在,又发现的请留言,我改正了也会更新 题目: PAT单位排行 (25 分) 每次 PAT 考试结束后,考试中心都会发布一个考生单位排行榜。本题就请你实现这个功能。 输入格式: 输入第一行给出一个正整数 N(≤10​5),即考生人数。随后 N 行,每行按下列格式给出一个考生的信息: 准考证号 得分 学校 其中准考证号是由 6 个字符组成的字符串,其首字母表示考...

熊仙森滴Blog 478

Vben Admin权限管理全解析:从动态菜单到按钮级控制(附代码示例)

本文全面解析Vben Admin框架的权限管理系统,从动态菜单生成到按钮级细粒度控制,提供完整的实现方案和代码示例。深入探讨基于角色-权限模型的架构设计,分享前后端协作的最佳实践,帮助开发者快速构建安全高效的企业级后台系统。

weixin_30919429的博客 485

2020.3.25——String.valueOf():char数组转变成字符串

import java.util.Scanner; import java.util.Stack; public class cg { public static void main(String args[]) { Scanner sc=new Scanner(System.in); char a[]= {'1','2','3','4','5'};**//int不行** ...

qq_45022670的博客 396

控制台模拟前端请求实现快速刷网课时长

本教程仅用于学习前端技术和网页开发,旨在提供教育与技术指导。对于任何未经授权或违反规定的使用行为,本人不承担任何责任。注意到那个serive后面有timestamp时间戳,暗示它很可能就是每30秒汇报一次进度和播放时长的命令,我们右键——复制——复制为fetch。有的网站上的网课需要的不仅仅是进度,而是时长,所以倍速插件失效,但我们可以使用控制台命令来模拟请求实现快速刷网课时长。尝试修改即可,例如我修改time为800,然后复制粘贴到控制台执行。播放视频,等待前端请求出现,然后复制前端请求为fetch。

m0_74823408的博客 2066

安川机器人SOCKET 通讯

命令 功能 参数 示例 返回 SelectJob 选择程序 JobName LineNo SelectJob TEST 0 #SelectJob,Ok StartJob 启动程序 JobName StartJob TEST #StartJob,Ok Pause 暂停当前程序 - Pause #Pause,Ok Continue 继续当前程序 - Continue #Continue,Ok ServoOn 伺服上电 - ServoOn #ServoOn,Ok ServoOf...

记录学习内容 8285

PAT 1028. List Sorting (25)

1028. List Sorting (25) Excel can sort records according to any column. Now you are supposed to imitate this function. Input Each input file contains one test case. For each case, the first ...

dengkonglou4529的博客 132

生物信息学新手必备:5分钟搞定GEO补充数据下载(含西柚云代理配置)

本文为生物信息学新手提供了快速下载GEO数据库补充数据的完整方案。核心在于解析GEO文件存储逻辑,构造正确的FTP下载链接,并利用云端代理服务(如西柚云)解决国内访问NCBI速度慢、易中断的问题。通过分步教程,用户无需命令行即可在5分钟内稳定获取大型测序文件,极大提升数据获取效率。

weixin_28314575的博客 637

PAT 甲级 1028 List Sorting【sort】

用C++的sort函数,这道题就是要写一个cmp函数 AC代码在最下面 ps:先说结论,涉及到连续操作内存的函数的时候,用char []不要用string。 在做的过程中有个问题,如果结构体的变量char []换成string,在cmp中加入输出语句会发现一些为空的比较情况,换句话说就是stu[]这个结构体数组,在和sort函数产生传值的行为时,会有传不过去的情况。 推测:问题应该和char [...

之虎者也的博客 172

RT-DETR改进策略【Conv和Transformer】| ICCV-2023 iRMB 倒置残差移动块 轻量化的注意力模块

本文记录的是利用模块优化的目标检测网络模型。的作用在于克服了常见模块无法同时吸收CNN 效率建模局部特征和利用Transformer 动态建模能力学习长距离交互的问题。相比一些复杂结构或多个混合模块的方法,能更好地权衡模型成本和精度。本文将其用于的模型改进和二次创新,更好地突出重要特征,提升模型性能。Rethinking Mobile Block for Efficient Attention-based Models从微观角度看,由和组成。及其改进的实现代码如下: 四、创新模块 4.1 改进点1⭐ 模块

Limiiiing的博客 816

【AI应用实战-claude】在飞书上接入claudecode进行远程使用(十三)

本文介绍了三种将ClaudeCode接入飞书的方案,包括官方飞书CLI工具(推荐)、cc-connect中转工具和MCP协议集成。官方飞书CLI适合自动化办公场景,通过简单配置即可实现消息发送和文档管理;cc-connect适用于远程调用Claude功能,通过飞书群聊触发响应;MCP协议则适合开发者构建复杂工作流。文章提供了详细的操作步骤,并针对不同使用场景给出了选择建议。

1013

PAT甲级1028 List Sorting:[C++题解]排序,cin和cout会超时

刷题

阿正的梦工坊 461

基于STM32+OneNet设计的物联网智慧路灯(100)

本次设计的智能路灯采用STM32芯片和OneNet物联网平台进行连接,开发出支持太阳能充电锂电池供电、GPS定位上传位置信息、手动/自动控制路灯开关、环境光照调节、红外传感器检测周围人员、空气质量监测等多项功能。同时,当路灯发生异常情况时,系统可以自动发送邮件或短信到指定邮箱或电话。

7727

C语言实现的简单公司职员信息管理系统

项目需求 设计要求 功能选择可以用菜单来实现,用户根据自己 的选择进入不同的菜单 程序不要求将信息保存到文件中,程序开始时职员信息为空,用户需要先增加职员信息,之后才可以实现查找、删除、浏览等功能。 职员信息的关键字为职员的职员号,加入时职员号重复的记录不能加入。查找删除按照职员号进行。 选择删除功能时输出提示信息"是否确定删除",可取消操作。 源代码 #define _CR...

余生愿你常欢笑 3914

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

PAT 1048 Find Coins (25分) 硬币面值做数组下标即排序

题目 Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins as payments. However, there was a special requirement of the payment: for

余生愿你常欢笑 2739

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

PAT 1004 Counting Leaves (30分) dfs求每层叶子节点数

题目 A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child. Input Specification: Each input file contains one test case. Each case starts with a line containing 0<N<100, the number of no

余生愿你常欢笑 1812

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

PAT 1008 Elevator (20分) 加减法难度级别

题目 The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one floor, and 4 seconds

余生愿你常欢笑 1501

PAT 1018 Public Bike Management (30分) 思路分析 + 满分代码

题目 There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city. The Public Bike Management Center (PBMC) keeps

余生愿你常欢笑 1409

PAT1047 Student List for Course (25分)避免运行超时(内存溢出)

题目 Zhejiang University has 40,000 students and provides 2,500 courses. Now given the registered course list of each student, you are supposed to output the student name lists of all the courses. Input Specification: Each input file contains one test case.

余生愿你常欢笑 1265

PAT 1023 Have Fun with Numbers (20分) 字符数组解决大整数存储溢出

题目 Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, with no duplication. Double it we will obtain 246913578, which happens to be another 9-digit number consisting exactly the numbers from 1 to 9, only in a di

余生愿你常欢笑 1182

PTA 1014 Waiting in Line (30分) 解题思路及满分代码

题目 Suppose a bank has N windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. The rules for the customers to wait in line are: The space inside the yellow line in front of each window is en

余生愿你常欢笑 1164

PAT 1021 Deepest Root (25分) 从测试点3超时到满分再到代码优化

题目 A graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you are supposed to find the root that results in a highest tree. Such a root is called the deepest root. Input Specification: Each

余生愿你常欢笑 915

PAT 1020 Tree Traversals (25分)思路分析 + 满分代码

题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree. Input Specification: Each inp

余生愿你常欢笑 734

PAT 1036 Boys vs Girls (25分) 比大小而已

题目 This time you are asked to tell the difference between the lowest grade of all the male students and the highest grade of all the female students. Input Specification: Each input file contains one test case. Each case contains a positive integer N, foll

余生愿你常欢笑 601

PAT 1026 Table Tennis (30分) 难度不高 + 逻辑复杂 +细节繁琐

题目 A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For any pair of players, if there are some tables open when they arrive, they will be assigned to the available table with the smallest number. If all the tab

余生愿你常欢笑 467

PAT 1017 Queueing at Bank (25分) 思路详解+满分代码

题目 Suppose a bank has K windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. All the customers have to wait in line behind the yellow line, until it is his/her turn to be served and there i

余生愿你常欢笑 395
上一篇: PAT 1006 Sign In and Sign Out (25分) 字符串比较
下一篇: PAT 1007 Maximum Subsequence Sum (25分) 最大连续子序列和
wwang_dev
博客等级 码龄7年 27粉丝 76原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值