• OpenJudge数据结构与算法-计算点的距离并排序


    /*==================================================================
    距离排序
    总时间限制: 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<iostream>
    #include<cmath>
    #include <iomanip>
    using namespace std;
    struct dian
    {
        int xx,yy,zz;
    };
    struct juLi
    {
        dian a,b;
        double len;
    };
    int main()
    {
        struct dian A[12];
        struct juLi B[50],TEMP;
        int n,i,j,t;
        int flag;
        freopen("4.in","r",stdin);
        cin>>n;
        for(i=0;i<n;i++)
        {
            cin>>A[i].xx>>A[i].yy>>A[i].zz;
        }
        t=0;
        for(i=0;i<n-1;i++)
        {
            for(j=i+1;j<n;j++)
            {
                B[t].a=A[i];
                B[t].b=A[j];
                B[t].len=sqrt((B[t].a.xx-B[t].b.xx)*(B[t].a.xx-B[t].b.xx)+
                              (B[t].a.yy-B[t].b.yy)*(B[t].a.yy-B[t].b.yy)+
                              (B[t].a.zz-B[t].b.zz)*(B[t].a.zz-B[t].b.zz));
                t++;
            }
        }
        
        for(i=1;i<t;i++)
        {
            flag=1;
            for(j=0;j<t-i;j++) 
            {
                if(B[j].len<B[j+1].len) 
                { 
                    flag=0;
                    TEMP=B[j]; 
                    B[j]=B[j+1]; 
                    B[j+1]=TEMP; 
                }
            }
            if(flag)  break;  //if(flag==1)  break;
        }
        
        for(i=0;i<t;i++)
        {
            cout<<'('<<B[i].a.xx<<','<<B[i].a.yy<<','<<B[i].a.zz<<')';
            cout<<'-';
            cout<<'('<<B[i].b.xx<<','<<B[i].b.yy<<','<<B[i].b.zz<<')';
            cout<<'=';
            cout<<setiosflags(ios::fixed)<<setprecision(2)<<B[i].len<<endl;
        }
        return 0;
    }
  • 相关阅读:
    在eclipse外边打开浏览器
    双开Eclipse
    6.5版本的CentOSLinux
    Intel x86_64 Architecture Background 3
    Java 大数、高精度模板
    Intel x86_64 Architecture Background 2
    Intel x86_64 Architecture Background 1
    Codeforces 999D Equalize the Remainders (set使用)
    Codeforces 996E Leaving the Bar (随机化)
    欧拉函数(小于或等于n的数中与n互质的数的数目)&& 欧拉函数线性筛法
  • 原文地址:https://www.cnblogs.com/huashanqingzhu/p/3450296.html
Copyright © 2020-2023  润新知