# POJ 3702 距离排序

126人阅读 评论(0)

….(n-1,n)

#include<iostream>
#include<iomanip>
#include<math.h>
#include<algorithm>
using namespace std;

#define maxp 12

struct points{
int x,y,z;
};

typedef struct{
points p1,p2;
double val;
}Dis_t;

bool operator <(Dis_t a, Dis_t b){
return a.val < b.val;
}

ostream & operator<<(ostream &output,Dis_t src){
//(0,0,0)-(1,1,1)=1.73
cout<<"("<<src.p1.x<<","<<src.p1.y<<","<<src.p1.z<<")-(";
cout<<src.p2.x<<","<<src.p2.y<<","<<src.p2.z<<")=";
cout<<fixed<<setprecision(2)<<src.val;
}

points point[maxp]={0};
Dis_t dis[maxp*maxp]={0};
int n;
int discount = 0;

double calcu(int i,int j){
double dis0;
double dx = point[i].x - point[j].x;
double dy = point[i].y - point[j].y;
double dz = point[i].z - point[j].z;
dis0 = sqrt(dx*dx+dy*dy+dz*dz);
//cout<<dis0<<endl;
return dis0;
}

int main(){
cin>>n;
for(int i=0;i<n;++i){
cin>>point[i].x>>point[i].y>>point[i].z;
}

for(int i=0;i<n-1;++i){
for(int j=i+1;j<n;++j){
Dis_t tmp;
tmp.p1 = point[i];
tmp.p2 = point[j];
tmp.val = calcu(i,j);
dis[discount++] = tmp;
}
}

//sort(dis,dis+discount);
for(int i=0;i<discount-1;++i){
for(int j =0;j<discount-i-1;++j){
if(dis[j]<dis[j+1]){
Dis_t t = dis[j];
dis[j] = dis[j+1];
dis[j+1] = t;
}
}
}

for(int i=0;i<discount;++i){
cout<<dis[i]<<endl;
}
//system("pause");
return 0;
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：4938次
• 积分：410
• 等级：
• 排名：千里之外
• 原创：39篇
• 转载：0篇
• 译文：0篇
• 评论：0条
评论排行