P5143 攀爬者(结构体+sort排序)
1.P5143 攀爬者(结构体+sort排序)
P5143 攀爬者
sort排序
- 对于数组而言
sort(数组+begin,数组+stop)(左闭右开)
例:
sort(a+1,a+n+1)=sort(a[1]~a[n])
对于结构体
在数组基础上多一个cmp
运用:sort(数组+begin,数组+stop,cmp) - cmp本身需要定义一个函数来表示比较。
bool cmp(结构体 b, 结构体 c) {
return b.z > c.z;
}
前大于后表示排序从大到小
前小于后表示排序从小到大
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
using namespace std;
struct mountain {
int x, y, z;
} a[50001];
bool cmp(mountain b, mountain c) {
return b.z > c.z;
}
double ans;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i].x >> a[i].y >> a[i].z;
}
sort(a + 1, a + n, cmp);
for (int i = 1; i <= n - 1; i++) {
ans += sqrt((a[i].x - a[i + 1].x) * (a[i].x - a[i + 1].x) + (a[i].y - a[i + 1].y) * (a[i].y - a[i + 1].y) +
(a[i].z - a[i + 1].z) * (a[i].z - a[i + 1].z));
}
printf("%.3f", ans);
return 0;
}
合集:
洛谷排序
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?