P5143 攀爬者
sort排序:
对于数组而言
sort(数组+begin,数组+stop)(左闭右开)
例:
sort(a+1,a+n+1)=sort(a[1]~a[n])
对于结构体
在数组基础上多一个cmp
运用:sort(数组+begin,数组+stop,cmp)
cmp本身需要定义一个函数来表示比较。
#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;
}
标签:sort,int,攀爬,数组,include,P5143,cmp
From: https://www.cnblogs.com/sdlypsck/p/17852642.html