蓝桥杯算法练习笔记(2)__Sort排序
发布日期:2021-04-30 21:05:51
浏览次数:119
分类:精选文章
本文共 3735 字,大约阅读时间需要 12 分钟。
C++ ????
1. ????
? C++ ?????????????????????????? <algorithm> ????? sort ??????????????
#include#include using namespace std;bool cmp(int x, int y) { return x > y; // ????}int main() { int a[10]; for (int i = 0; i < 10; i++) { cin >> a[i]; } sort(a, a + 10); // ???? for (int i = 0; i < 10; i++) { cout << a[i] << " "; } cout << endl; sort(a, a + 10, greater<>()); // ???? for (int i = 0; i < 10; i++) { cout << a[i] << " "; } cout << endl;}
?????????????????????? greater<>() ?????????
2. ?????
C++ ????????????????????????????????????
#include#include using namespace std;struct Student { int score; string name; Student(string n, int s) : name(n), score(s) {}};int main() { Student stu[3]; for (int i = 0; i < 3; i++) { string n; int s; cin >> n >> s; stu[i] = Student(n, s); } sort(stu, stu + 3, [](Student x, Student y) { return x.name < y.name; }); for (int i = 0; i < 3; i++) { cout << stu[i].name << ":" << stu[i].score << " "; } cout << endl;}
????????? Student ????????????????????
3. ?????
??????????????
#include#include #include using namespace std;struct Student { string name; int score[4];};bool cmp(Student x, Student y) { if (x.score[0] != y.score[0]) { return x.score[0] > y.score[0]; } if (x.score[1] != y.score[1]) { return x.score[1] > y.score[1]; } if (x.score[2] != y.score[2]) { return x.score[2] > y.score[2]; } return x.score[3] > y.score[3];}int main() { Student stu[3]; for (int i = 0; i < 3; i++) { cin >> stu[i].name; for (int j = 0; j < 4; j++) { cin >> stu[i].score[j]; } } sort(stu, stu + 3, cmp); for (int i = 0; i < 3; i++) { cout << stu[i].name << ":"; for (int j = 0; j < 4; j++) { cout << stu[i].score[j] << " "; } cout << endl; }}
?????????????????????
4. ?????
???????????????????????????????????
#include#include #include using namespace std;const double ERSILON = 1e-6;bool cmp(double a, double b) { double da = fabs(a - round(a)); double db = fabs(b - round(b)); if (fabs(da - db) < ERSILON) { return a < b; } return da < db;}int main() { int n; cin >> n; double num[n]; for (int i = 0; i < n; i++) { cin >> num[i]; } sort(num, num + n, cmp); for (int i = 0; i < n; i++) { cout << num[i] << " "; } cout << endl;}
??????????????????????????
5. ????
?????????????
#include#include using namespace std;int main() { int N, l1, r1, l2, r2; cin >> N >> l1 >> r1 >> l2 >> r2; int num[10005]; for (int i = 0; i < N; i++) { cin >> num[i]; } sort(num + l1 - 1, num + r2); // ?? sort(num + l2 - 1, num + r2, greater<>()); // ?? for (int i = 0; i < N; i++) { cout << num[i] << " "; } cout << endl;}
???????????????????????????
6. Vector ?? Sort
? C++ ??vector ??? sort ?????????
#include#include #include using namespace std;int main() { vector ve; for (int i = 10; i > 0; i--) { ve.push_back(i); } cout << "???-----"; for (int i = 0; i < 10; i++) { cout << ve[i] << " "; } cout << endl; sort(ve.begin(), ve.end()); cout << "???-----"; for (int i = 0; i < 10; i++) { cout << ve[i] << " "; } cout << endl;}
?????? sort ? vector ?????
发表评论
最新留言
感谢大佬
[***.8.128.20]2026年06月16日 07时17分36秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
PHP实现微信公众号H5支付
2023-03-01
PHP实现微信公众号网页授权
2023-03-01
PHP实现微信小程序推送消息至公众号
2023-03-01
rabbitmq逻辑与开发
2023-03-01
php实现根据身份证获取年龄
2023-03-01
PHP实现的MongoDB数据增删改查
2023-03-01
PHP实现的SSO单点登录系统,拿走就用吧
2023-03-01
php实现短信验证功能
2023-03-01
php实现逆转数组
2023-03-01
PHP实现通过geoip获取IP地理信息
2023-03-01
PHP实现页面静态化、纯静态化及伪静态化
2023-03-01
php容许ajax跨域,PHP设置允许ajax跨域请求的两种常见方法
2023-03-01
RabbitMQ进程结构分析与性能调优
2023-03-01
PHP对接百度地图
2023-03-01
PHP对表单提交特殊字符的过滤和处理
2023-03-01
php对象引用和析构函数的关系
2023-03-01
RabbitMQ HTTP 认证后端项目常见问题解决方案
2023-03-01
PHP将图片转换成base64格式(优缺点)
2023-03-01
php将多个值的数组去除重复元素
2023-03-01