P1081最近距离

    技术2022-05-19  25

     

    描述 Description    在一块地上,有着n(1<=n<=2000) 头牛,输入n,再分别输入这n头牛的坐标(x,y) (1<=x<=100000,1<=y<=100000),如果第i头牛与第j头牛间的距离最近,那么输出i和j                      10 | . . . . . . . 3 . . . . .           9 | . 1 . . 2 . . . . . . . .           8 | . . . . . . . . . . . . .           7 | . . . . . . . . . . 4 . .           6 | . . . . . . 9 . . . . . .           5 | . 8 . . . . . . . . . . .           4 | . . . . . 7 . . . . . . .           3 | . . . . . . . . . 5 . . .           2 | . . . . . . . . . . . . .           1 | . . . . 6 . . . . . . . .           0 ---------------------------                       1 1 1 1             0 1 2 3 4 5 6 7 8 9 0 1 2 3

    输入格式 Input Format  第一行n下面n行,x,y        输出格式 Output Format  最近的两个点        样例输入 Sample Input [复制数据]     

     

     

        样例输出 Sample Output [复制数据]   

     

     

     

     

     

     

    #include <iostream>

    #include <cstdio>

    #include <cstring>

    using namespace std;

    long long x[2050],y[2050];

    int main ()

    {

        int n;

        cin>>n;

        for(int i=1;i<=n;i++) cin>>x[i]>>y[i];

        long long min=((long long)1<<63)-1;                   //最大数的表示方法

        int a=1,b=1;

        for(int i=1;i<n;i++)

        for(int j=i+1;j<=n;j++)

        {

            long long l=(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);

            if(l<min)

            {

                min=l;

                a=i;

                b=j;

            }

        }

        cout<<a<<' '<<b<<endl;

        return 0;

    }


    最新回复(0)