距离排序

1:距离排序查看提交统计提问总时间限制: 1000ms 内存限制: 65536kB描述给出三维空间中的n个点(不超过10个),求出n个点两两之间的距离,并按距离由大到小依次输出两个点的坐标及它们之间的距离。
输入输入包括两行,第一行包含一个整数n表示点的个数,第二行包含每个点的坐标(坐标都是整数)。点的坐标的范围是0到100,输入数据中不存在坐标相同的点。输出对于大小为n的输入数据,输出n*(n-1)/2行格式如下的距离信息:
(x1,y1,z1)-(x2,y2,z2)=距离
其中距离保留到数点后面2位。
(用cout输出时保留到小数点后2位的方法:cout<<fixed<<setprecision(2)<<x)样例输入4
0 0 0 1 0 0 1 1 0 1 1 1样例输出(0,0,0)-(1,1,1)=1.73
(0,0,0)-(1,1,0)=1.41
(1,0,0)-(1,1,1)=1.41
(0,0,0)-(1,0,0)=1.00
(1,0,0)-(1,1,0)=1.00
(1,1,0)-(1,1,1)=1.00提示用cout输出时保留到小数点后2位的方法:cout<<fixed<<setprecision(2)<<x

注意:
冒泡排序满足下面的性质,选择排序和快速排序(qsort或sort)需要对下面的情况进行额外处理
使用冒泡排序时要注意边界情况的处理,保证比较的两个数都在数组范围内

  1. 对于一行输出中的两个点(x1,y1,z1)和(x2,y2,z2),点(x1,y1,z1)在输入数据中应出现在点(x2,y2,z2)的前面。
    比如输入:
    2
    0 0 0 1 1 1
    输出是:
    (0,0,0)-(1,1,1)=1.73
    但是如果输入:
    2
    1 1 1 0 0 0
    输出应该是:
    (1,1,1)-(0,0,0)=1.73

  2. 如果有两对点p1,p2和p3,p4的距离相同,则先输出在输入数据中靠前的点对。
    比如输入:
    3
    0 0 0 0 0 1 0 0 2
    输出是:
    (0,0,0)-(0,0,2)=2.00
    (0,0,0)-(0,0,1)=1.00
    (0,0,1)-(0,0,2)=1.00
    如果输入变成:
    3
    0 0 2 0 0 1 0 0 0
    则输出应该是:
    (0,0,2)-(0,0,0)=2.00
    (0,0,2)-(0,0,1)=1.00
    (0,0,1)-(0,0,0)=1.00

#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
struct dis
{
    int x1, y1, z1;
    int x2, y2, z2;
    double v;
    void init(int a, int b, int c, int d, int e, int f)
    {
        x1 = a;
        y1 = b;
        z1 = c;
        x2 = d;
        y2 = e;
        z2 = f;
        v = sqrt(1.0 * ((a-d)*(a-d)+(b-e)*(b-e)+(c-f)*(c-f)));
    }

} a[105];
int x[12], y[12], z[12];

int divide(int left, int right)
{
    int i = left;
    int j = right;
    dis k = a[i];
    while (i != j)
    {
        while (i < j && a[j].v <= k.v)
        {
            j--;
        }
        if (j > i)
        {
            a[i] = a[j];
            i++;
        }
        while (i < j && a[i].v >= k.v)
        {
            i++;
        }
        if (i < j)
        {
            a[j] = a[i];
            j--;
        }
    }
    a[i] = k;
    return i;
}

void mysort(int left, int right)
{
    if (left < right)
    {
        int m = divide(left, right);
        mysort(left, m);
        mysort(m + 1, right);
    }
}

int main()
{
  /*  freopen("myin.txt", "r", stdin);
    freopen("myout.txt", "w", stdout);*/
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> x[i] >> y[i] >> z[i];
    }
    int t = 0;
    for (int i = 0; i < n; i++)
    {
        for (int j = i + 1; j < n; j++)
        {
            a[t++].init(x[i], y[i], z[i], x[j], y[j], z[j]);
        }
    }
    /*for (int i = 0; i < t; i++)
    {
        cout << '(' << a[i].x1 << ',' << a[i].y1 << ',' << a[i].z1 << ')' << '-' << '(' << a[i].x2 << ',' << a[i].y2 << ',' << a[i].z2 << ')' << '=' << fixed << setprecision(2) << a[i].v << endl;
    }
    cout << endl;*/
    dis k;
    for (int i = 0; i < t; i++)
    {
        bool yes = 0;
        for (int j = t - 1; j > i; j--)
        {
            if(a[j].v >a[j - 1].v)
            {
                yes = 1;
                k = a[j];
                a[j] = a[j-1];
                a[j - 1] = k;
            }
        }
        if(!yes)
        {
            break;
        }
    }

        //mysort(0, t - 1);
        for (int i = 0; i < t; i++)
        {
            cout << '(' << a[i].x1 << ',' << a[i].y1 << ',' << a[i].z1 << ')' << '-' << '(' << a[i].x2 << ',' << a[i].y2 << ',' << a[i].z2 << ')' << '=' << fixed << setprecision(2) << a[i].v << endl;
        }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值