结构体排序使用sort要写cmp来判断
例题 洛谷P5143
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
struct point
{
int x;
int y;
int z;
};
int cmp(point a,point b)
{
if(a.z<b.z)
{
return 1;
}
else
{
return 0;
}
}
int main()
{
int n,i,x1,y1,z1,x2,y2,z2;
double ans;
struct point a[50001];
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d %d %d",&a[i].x,&a[i].y,&a[i].z);
}
sort(a,a+n,cmp);
ans = 0;
x1 = a[0].x;
y1 = a[0].y;
z1 = a[0].z;
for(i=1;i<n;i++)
{
x2 = a[i].x;
y2 = a[i].y;
z2 = a[i].z;
ans += sqrt(pow(x2-x1,2)+pow(y2-y1,2)+pow(z2-z1,2));
x1 = x2;
y1 = y2;
z1 = z2;
}
printf("%.3f",ans);
return 0;
}