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 是 ...
随机推荐
- mycat1.6.5分片(字符串拆分hash)
https://blog.csdn.net/webnum/article/details/78313525 分片规则:字符串拆分hash 一.conf/schema.xml文件 <?xm ...
- 错误:正在解析文件 '/var/lib/dpkg/updates/0004'
sudo rm /var/lib/dpkg/updates/* sudo apt-get update
- C# String.Format方法
一.定义String.Format是将指定的 String类型的数据中的每个格式项替换为相应对象的值的文本等效项. 如: (1)string p1 = "Jackie";strin ...
- QQ的全国地址编码
D:\Program Files (x86)\Tencent\QQ\I18N\2052\LocList.xml
- Storm概念学习系列之Tuple元组(数据载体)
不多说,直接上干货! Tuple元组 Tuple 是 Storm 的主要数据结构,并且是 Storm 中使用的最基本单元.数据模型和元组. Tuple 描述 Tuple 就是一个值列表, Tuple ...
- Cmder 简明使用说明
简介 Cmder is a software package created out of pure frustration over the absence of nice console emul ...
- 属性(property)与成员变量(ivar)
类内使用成员变量{}, 类外使用属性@property /*********** --- Person.h */ @interface Person : NSObject { NSString *_n ...
- spring batch 以游标的方式 数据库读取数据 然后写入目标数据库
前面关于Spring Batch的文章,讲述了SpringBatch对Flat.XML等文件的读写操作,本文将和大家一起讨论Spring Batch对DB的读写操作.Spring Batch对DB数据 ...
- 学习笔记:《JavaScript高级程序设计》
第1章 JavaScript简介 1.一个完整的JavaScript实现应该由三部分组成:核心(ECMAScript),文档对象模型(DOM)和浏览器对象模型(BOM). 2.Web浏览器只是ECMA ...
- iOS 力学动画生成器UIKit Dynamics 之碰撞效果讲解
UIKit Dynamic是iOS7 新增的一组类和方法,可赋予UIView逼真的行为和特征,不需要写动画效果那些繁琐的代码,让开发人员能够轻松地改善应用的用户体验.一共有6个可用于定制UIDynam ...