PAT 乙级 1015
题目
题目地址:PAT 乙级 1015
题解
常规题,难点在于理清楚排序规则,通过比较简洁的方式进行编码;
在这里我选择使用vector进行存储,并使用sort方法排序,因为本题不是简单按照大小排序,因此这里需要再次理解sort方法中的第三个参数,以及它的使用细节;
关于sort的具体使用方法,可以在我之前的博文查看;
还需要最后说明的一点问题是:尽量使用 printf 和 scanf,减少 cout 和 cin 的使用,之前用cin和cout,结果时间超限了……
代码
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; struct Stu {
int num;
int s1;
int s2;
}; bool comp(Stu x, Stu y) {
if (x.s1 + x.s2 > y.s1 + y.s2) return true;
else if (x.s1 + x.s2 == y.s1 + y.s2 && x.s1 > y.s1) return true;
else if (x.s1 + x.s2 == y.s1 + y.s2 && x.s1 == y.s1 && x.num < y.num) return true;
else return false;
} int main() {
vector<Stu> stu1, stu2, stu3, stu4;
int tmp_num, tmp_s1, tmp_s2;
Stu tmp_stu;
int N = , L = , H = ;
scanf("%d %d %d", &N, &L, &H);
while (N--) {
scanf("%d %d %d", &tmp_num, &tmp_s1, &tmp_s2);
tmp_stu.num = tmp_num;
tmp_stu.s1 = tmp_s1;
tmp_stu.s2 = tmp_s2;
if (tmp_stu.s1 >= L && tmp_stu.s2 >= L) {
if (tmp_stu.s1 >= H && tmp_stu.s2 >= H) stu1.push_back(tmp_stu);
else if (tmp_stu.s1 >= H && tmp_stu.s2 < H) stu2.push_back(tmp_stu);
else if (tmp_stu.s1 < H && tmp_stu.s2 < H && tmp_stu.s1 >= tmp_stu.s2) stu3.push_back(tmp_stu);
else stu4.push_back(tmp_stu);
}
}
sort(stu1.begin(), stu1.end(), comp);
sort(stu2.begin(), stu2.end(), comp);
sort(stu3.begin(), stu3.end(), comp);
sort(stu4.begin(), stu4.end(), comp);
printf("%d\n", stu1.size() + stu2.size() + stu3.size() + stu4.size());
for (int i = ; i < stu1.size(); i++)
printf("%d %d %d\n", stu1[i].num, stu1[i].s1, stu1[i].s2);
for (int i = ; i < stu2.size(); i++)
printf("%d %d %d\n", stu2[i].num, stu2[i].s1, stu2[i].s2);
for (int i = ; i < stu3.size(); i++)
printf("%d %d %d\n", stu3[i].num, stu3[i].s1, stu3[i].s2);
for (int i = ; i < stu4.size(); i++)
printf("%d %d %d\n", stu4[i].num, stu4[i].s1, stu4[i].s2); return ;
}
这里的代码感觉还是有些冗余,尤其是排序和输入的部分,因此再贴上经过微小改进后的代码;
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; struct Stu {
int num;
int s1;
int s2;
}; bool comp(Stu x, Stu y) {
if (x.s1 + x.s2 > y.s1 + y.s2) return true;
else if (x.s1 + x.s2 == y.s1 + y.s2 && x.s1 > y.s1) return true;
else if (x.s1 + x.s2 == y.s1 + y.s2 && x.s1 == y.s1 && x.num < y.num) return true;
else return false;
} int main() {
vector<Stu> stu[];
int tmp_num, tmp_s1, tmp_s2;
Stu tmp_stu;
int N = , L = , H = ;
scanf("%d %d %d", &N, &L, &H);
while (N--) {
scanf("%d %d %d", &tmp_num, &tmp_s1, &tmp_s2);
tmp_stu.num = tmp_num;
tmp_stu.s1 = tmp_s1;
tmp_stu.s2 = tmp_s2;
if (tmp_stu.s1 >= L && tmp_stu.s2 >= L) {
if (tmp_stu.s1 >= H && tmp_stu.s2 >= H) stu[].push_back(tmp_stu);
else if (tmp_stu.s1 >= H && tmp_stu.s2 < H) stu[].push_back(tmp_stu);
else if (tmp_stu.s1 < H && tmp_stu.s2 < H && tmp_stu.s1 >= tmp_stu.s2) stu[].push_back(tmp_stu);
else stu[].push_back(tmp_stu);
}
}
printf("%d\n", stu[].size() + stu[].size() + stu[].size() + stu[].size());
for (int i = ; i < ; i++) {
sort(stu[i].begin(), stu[i].end(), comp);
for (int j = ; j < stu[i].size(); j++)
printf("%d %d %d\n", stu[i][j].num, stu[i][j].s1, stu[i][j].s2);
} return ;
}
这样整体代码就简洁了一些,看起来也没有那么臃肿
PAT 乙级 1015的更多相关文章
- PAT乙级1015
题目链接 https://pintia.cn/problem-sets/994805260223102976/problems/994805307551629312 题解 思路比较简单,核心就是定义一 ...
- PAT乙级 1015. 德才论 (25)
1015. 德才论 (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Li 宋代史学家司马光在<资治通鉴&g ...
- PAT——乙级1015/甲级1062:德才论
这两个题是一模一样的 1015 德才论 (25 point(s)) 宋代史学家司马光在<资治通鉴>中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德 ...
- pat 乙级 1015. 德才论 (25) c++
http://39.106.25.239 个人网站 欢迎访问 交流 1015. 德才论 (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Sta ...
- C#版 - PAT乙级(Basic Level)真题 之 1021.个位数统计 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - P ...
- PAT乙级真题及训练题 1025. 反转链表 (25)
PAT乙级真题及训练题 1025. 反转链表 (25) 感觉几个世纪没打代码了,真是坏习惯,调了两小时把反转链表调出来了,心情舒畅. 这道题的步骤 数据输入,数组纪录下一结点及储存值 创建链表并储存上 ...
- PAT 乙级 1024
题目 题目地址:PAT 乙级 1024 题解 模拟题,重点需要考虑到各种不同情况:简单来说一下: 因为输入格式固定,所以把不同的部分分别存储和处理可以在很大程度上简化运算:其中需要考虑最多的就是小数部 ...
- PAT 乙级 1017
题目 题目地址:PAT 乙级 1017 题解 粗看是一道大数除法题,实际上只不过是通过字符数组模拟除法过程,理解之后还是比较简单的: 具体分析一下本题: 因为题设中的除数(n)是一位整数,因此大幅简化 ...
- PAT 乙级 1003
题目 题目地址:PAT 乙级 1003 题解 规律观察题,本题的关键在于把题读懂,同时还有几个比较容易疏忽的地方需要注意:总之这道题要考虑的东西更多,细节上也要特别注意: 规律:“如果 aPbTc 是 ...
随机推荐
- Tomcat从socket到java Servlet
整体架构图 一. 启动阶段 BootStrap的main方法加载server.xml配置文件,封装成Server,Service,Connector,Engine等java对象 Server初始化== ...
- sql新增字段注意事项
新增字段的类型.长度(精度)是否合适 解决方法: 跟应用明确加字段和改字段的风险,确认新增字段类型正确.长度(精度)合适. 以及跟应用明确老数据是否要订正?如何订正?新增列是否非空?是否有默认值等等. ...
- linux目录权限
linux中,有三种不同类型的用户可以对文件或目录进行访问:文件所有者,同组用户,其他用户.所有者一般是文件的创建者,文件所有者自动拥有对该文件的读.写和可执行权限.所有者能允许同组用户有权访问文件, ...
- D. Time to Raid Cowavans 分块暴力,感觉关键在dp
http://codeforces.com/contest/103/problem/D 对于b大于 sqrt(n)的,暴力处理的话,那么算出每个的复杂度是sqrt(n),就是把n分成了sqrt(n)段 ...
- Java面向对象_常用类库api——二叉树数据结构实现
二叉树是每个节点最多有两个子树的有序树.通常子树被称为"左子树"和"右子树". 二叉树算法的排序规则: 1.选择第一个元素作为根节点 2.之后如果元素大于根节点 ...
- mysql连接error,Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection .....
完整error Establishing SSL connection without server's identity verification is not recommended. Accor ...
- 八个cmd 命令
一,ping 它是用来检查网络是否通畅或者网络连接速度的命令.作为一个生活在网络上的管理员或者黑客来说,ping命令是第一个必须掌握的DOS命令,它所利用的原理是这样的:网络上的机器都有唯一确定的IP ...
- java数据结构和算法07(2-3-4树)
上一篇我们大概了解了红黑树到底是个什么鬼,这篇我们可以看看另外一种树-----2-3-4树,看这个树的名字就觉得很奇怪.... 我们首先要知道这里的2.3.4指的是任意一个节点拥有的子节点个数,所以我 ...
- JAVA中面向对象
一.方法: 1.方法概述: 在JAVA中,方法就是用来完成解决某件事情或实现某个功能的办法. 2.方法的语法格式: 修饰符 返回值类型 方法名(参数类型 参数名1,参数类型 参数名2,.....){ ...
- 一、 Spring IOC 简介
一 . 什么是IOC(Inversion of Control) IOC :控制反转,也称为依赖注入(DI).听起来有点云里雾里,他到底是个啥,为什么Spirng中最基础的就是它? 我们的项目其实都是 ...