P5143 题解
传送门:P5143 攀爬者
更佳的阅读体验:洛谷 P5143 题解
简要题意:给定
我们可以创建一个坐标结构体,用于存放每个点的
对于排序规则,下文代码中使用了重载 < 运算符,它等价于编写一个自定义排序函数。编写自定义排序函数的做法将以注释的形式给出。
#include <iostream>
#include <algorithm>
#include <cmath>
#include <iomanip>
using namespace std;
const int N = 5e4 + 10;
int n;
struct pos {
double x, y, z;
bool operator<(const pos x) const {
return z < x.z;
}
} a[N];
double ans;
/* bool cmp(pos a, pos b) {
return a.z < b.z;
} */
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i].x >> a[i].y >> a[i].z;
sort(a + 1, a + n + 1);
// sort(a + 1, a + n + 1, cmp);
for (int i = 1; i < n; ++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));
cout << fixed << setprecision(3) << ans << '\n';
return 0;
}