当前位置: 首页 > news >正文

2025-7-15-C++ 学习 排序(4)

文章目录

  • 2025-7-15-C++ 学习 排序(4)
  • P5143 攀爬者
    • 题目背景
    • 题目描述
    • 输入格式
    • 输出格式
    • 输入输出样例 #1
      • 输入 #1
      • 输出 #1
    • 说明/提示
    • 提交代码
  • P1104 生日
    • 题目描述
    • 输入格式
    • 输出格式
    • 输入输出样例 #1
      • 输入 #1
      • 输出 #1
    • 说明/提示
    • 提交代码
  • P1012 [NOIP 1998 提高组] 拼数
    • 题目描述
    • 输入格式
    • 输出格式
    • 输入输出样例 #1
      • 输入 #1
      • 输出 #1
    • 输入输出样例 #2
      • 输入 #2
      • 输出 #2
    • 说明/提示
    • 提交代码

2025-7-15-C++ 学习 排序(4)

  排序,end。

P5143 攀爬者

题目背景

HKE 考完 GDOI 之后跟他的神犇小伙伴们一起去爬山。

题目描述

他在地形图上标记了 NNN 个点,每个点 PiP_iPi 都有一个坐标 (xi,yi,zi)(x_i,y_i,z_i)(xi,yi,zi)。所有点对中,高度值 zzz 不会相等。HKE 准备从最低的点爬到最高的点,他的攀爬满足以下条件:

(1) 经过他标记的每一个点;

(2) 从第二个点开始,他经过的每一个点高度 zzz 都比上一个点高;

(3) HKE 会飞,他从一个点 PiP_iPi 爬到 PjP_jPj 的距离为两个点的欧几里得距离。即,(Xi−Xj)2+(Yi−Yj)2+(Zi−Zj)2\sqrt{(X_i-X_j)^2+(Y_i-Y_j)^2+(Z_i-Z_j)^2}(XiXj)2+(YiYj)2+(ZiZj)2

现在,HKE 希望你能求出他攀爬的总距离。

输入格式

第一行,一个整数 NNN 表示地图上的点数。

接下来 NNN 行,三个整数 xi,yi,zix_i,y_i,z_ixi,yi,zi 表示第 iii 个点的坐标。

输出格式

一个实数,表示 HKE 需要攀爬的总距离(保留三位小数)

输入输出样例 #1

输入 #1

5
2 2 2
1 1 1
4 4 4
3 3 3
5 5 5

输出 #1

6.928

说明/提示

对于100%的数据,1≤N≤500001\leq N\leq 500001N50000,答案的范围在 double 范围内。

提交代码

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iomanip>
using namespace std;struct Point {int x, y, z;
};bool compare(const Point& a, const Point& b) {return a.z < b.z;
}double distance(const Point& a, const Point& b) {return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2) + pow(a.z - b.z, 2));
}int main() {int n;cin >> n;vector<Point> points(n);for (int i = 0; i < n; i++) {cin >> points[i].x >> points[i].y >> points[i].z;}sort(points.begin(), points.end(), compare);double totalDistance = 0.0;for (int i = 0; i < n - 1; i++) {totalDistance += distance(points[i], points[i + 1]);}cout << fixed << setprecision(3) << totalDistance << endl;return 0;
}

P1104 生日

题目描述

cjf 君想调查学校 OI 组每个同学的生日,并按照年龄从大到小的顺序排序。但 cjf 君最近作业很多,没有时间,所以请你帮她排序。

输入格式

输入共有 n+1n + 1n+1 行,

111 行为 OI 组总人数 nnn

222 行至第 n+1n+1n+1 行分别是每人的姓名 sss、出生年 yyy、月 mmm、日 ddd

输出格式

输出共有 nnn 行,

nnn 个生日从大到小同学的姓名。(如果有两个同学生日相同,输入靠后的同学先输出)

输入输出样例 #1

输入 #1

3
Yangchu 1992 4 23
Qiujingya 1993 10 13
Luowen 1991 8 1

输出 #1

Luowen
Yangchu
Qiujingya

说明/提示

数据保证,1<n<1001<n<1001<n<1001≤∣s∣<201\leq |s|<201s<20。保证年月日实际存在,且年份 ∈[1960,2020]\in [1960,2020][1960,2020]

提交代码

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;struct Student {string name;int year, month, day;int order;
};bool compare(const Student& a, const Student& b) {if (a.year != b.year) return a.year < b.year;if (a.month != b.month) return a.month < b.month;if (a.day != b.day) return a.day < b.day;return a.order > b.order;
}int main() {int n;cin >> n;vector<Student> students(n);for (int i = 0; i < n; i++) {cin >> students[i].name >> students[i].year >> students[i].month >> students[i].day;students[i].order = i;}sort(students.begin(), students.end(), compare);for (const auto& s : students) {cout << s.name << endl;}return 0;
}

P1012 [NOIP 1998 提高组] 拼数

题目描述

设有 nnn 个正整数 a1…ana_1 \dots a_na1an,将它们联接成一排,相邻数字首尾相接,组成一个最大的整数。

输入格式

第一行有一个整数,表示数字个数 nnn

第二行有 nnn 个整数,表示给出的 nnn 个整数 aia_iai

输出格式

一个正整数,表示最大的整数

输入输出样例 #1

输入 #1

3
13 312 343

输出 #1

34331213

输入输出样例 #2

输入 #2

4
7 13 4 246

输出 #2

7424613

说明/提示

对于全部的测试点,保证 1≤n≤201 \leq n \leq 201n201≤ai≤1091 \leq a_i \leq 10^91ai109

NOIP1998 提高组 第二题

提交代码

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>using namespace std;bool compare(const string &a, const string &b) {return a + b > b + a;
}int main() {int n;cin >> n;vector<string> nums(n);for (int i = 0; i < n; i++) {cin >> nums[i];}sort(nums.begin(), nums.end(), compare);string result;for (const string &num : nums) {result += num;}cout << (result[0] == '0' ? "0" : result) << endl;return 0;
}

文章转载自:
http://antelope.hfytgp.cn
http://agnolotti.hfytgp.cn
http://castalie.hfytgp.cn
http://chemical.hfytgp.cn
http://cholagogue.hfytgp.cn
http://bulbiform.hfytgp.cn
http://calligraphist.hfytgp.cn
http://auxotrophy.hfytgp.cn
http://beltway.hfytgp.cn
http://arno.hfytgp.cn
http://billboard.hfytgp.cn
http://bravo.hfytgp.cn
http://blackmail.hfytgp.cn
http://amsterdam.hfytgp.cn
http://agglutinate.hfytgp.cn
http://accipiter.hfytgp.cn
http://ceremonialize.hfytgp.cn
http://aphoxide.hfytgp.cn
http://censurable.hfytgp.cn
http://carrageenan.hfytgp.cn
http://anime.hfytgp.cn
http://aristotype.hfytgp.cn
http://backlot.hfytgp.cn
http://aflame.hfytgp.cn
http://astrobotany.hfytgp.cn
http://auditoria.hfytgp.cn
http://chloride.hfytgp.cn
http://chanteyman.hfytgp.cn
http://benthamite.hfytgp.cn
http://airward.hfytgp.cn
http://www.dtcms.com/a/280701.html

相关文章:

  • langchain教程10:LCEL
  • 【c++】c++11新特性(右值引用和移动语义)
  • PySpark 常用算子详解
  • 【BUG处理】构建APK时遇到错误:‘flutter‘ 命令未被识别。这通常表示您的系统中未安装Flutter SDK或环境变量配置不正确。
  • 牛客:HJ20 密码验证合格程序[华为机考][字符串]
  • 【源力觉醒 创作者计划】文心4.5 vs DeepSeek vs Qwen 3.0:三大能力硬核实测!谁才是王者?
  • 纸板加工制造学习1
  • CF37E Trial for Chief 题解
  • 青年科学基金项目答辩PPT模板 | 杰青优青ppt设计制作美化 | WordinPPT
  • uni-app 学习笔记:Vuex 持久化数据
  • 【C++】神奇的AVL树
  • Java单元测试JUnit
  • 使用 Java 获取 PDF 页面信息(页数、尺寸、旋转角度、方向、标签与边框)
  • 已知均数与标准差,如何生成一组正态分布数据?
  • EPLAN 电气制图(九):直流电源绘制+端子排绘制
  • 线程(二) linux 互斥
  • JVM——有哪些常见的垃圾收集器
  • Props
  • 时序数据库与AI的融合:智能时代的数据基石
  • 027_国际化与本地化
  • Spring应用抛出NoHandlerFoundException、全局异常处理、日志级别
  • FreeRTOS学习笔记——移植说明、任务创建
  • 【Ubuntu22.04】repo安装方法
  • Linux715 磁盘管理:逻辑卷
  • 聊聊MySQL中的buffer pool
  • Spring Boot目录变文件夹?3步解决!
  • Unity Editor下拉框,支持搜索,多层级
  • BGP服务器和多线服务器的不同之处
  • Python初学者笔记第十三期 -- (常用内置函数)
  • 原点安全签约金网络数科,共建一体化数据安全防护体系