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 是 ...
随机推荐
- (二)异步解决方案之callback
回调定义 就是一个函数里面使用 作为参数的函数. Function1(Function2) { Function2(); }; 同步调用 - 老实说,这和我们 不将代码封装成 函数没有差别 也就是说 ...
- ELK(ElasticSearch, Logstash, Kibana) v5.3.2 分布式日志收集分析最佳解决方案 基于CentOS 7 ( 一 )
[root@localhost temp]# .tar.gz -C /usr/local/ [root@localhost temp]# vim logstatsh_test.conf ======= ...
- 5 - ByteBuf-替换ByteBuffer-字节操作
a). 容量可按需增长,类似(StringBuilder) b). 读写模式之间不需要flip切换 c). 读和写使用不同的索引-writerIndex/readerIndex d). 支持方法的链式 ...
- Dede友情链接和分页列表和内容分页去掉小圆点LI标签
我用了一个比较老式的模板,友情链接和列表页底下的一段分页导航会自动获取LI标签,导致错位.每段文字前还多出一个可恶的黑色实心小圆点,心想肯定是LI标签在搞怪,于是把模板文件和样式文件都翻看了一个底朝天 ...
- shell 文件测试 蛮全的
文件状态测试 -b filename : 当filename 存在并且是块文件时返回真(返回0)-c filename : 当filename 存在并且是字符文件时返回真-d pathname : 当 ...
- Android 中文 API (101) —— AsyncTask
一.结构 public abstract class AsyncTask extends Object java.lang.Object android.os.AsyncTask<Params, ...
- WPF OnApplyTemplate 不执行 或者执行滞后的疑惑
OnApplyTemplate 不执行 平时如何开发自定义控件的 在WPF自定义控件开发的过程中遇到了这样一个问题,属性更改事件在OnApplyTemplate之前执行.我在写自定义控件的时候,喜欢通 ...
- mybatis-plus 异常 Invalid bound statement (not found)
最近吧项目中添加使用了mybatis-plus,发现操作sql的时候出现异常: Invalid bound statement (not found) ,异常位置位于mybatis-plus的jar中 ...
- cmd对其他盘符进行操作
一般我们打开cmd命令时,会出现如下界面: 现在,我想要对g盘进行操作,则输入 --> g:,然后回车,如图: 可以查看一下g盘下的所有子目录,输入 --> dir,回车,结果如下: 我想 ...
- 根据accept-language自动设置UICulture和Culture
在web.config中添加如下配置: <system.web> <globalization uiCulture="auto" culture="au ...