蛮力法解决最近对问题

跑个O(n^2),没啥可说的,直接上代码和数据。

C++代码

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;

const int maxn = 10005;

typedef struct Point
{
    double x;
    double y;
} point;

point p[maxn];

double getlen(point p1, point p2)
{
    return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
}

int main()
{
    int n;
    while (cin >> n && n)
    {
        for (int i = 0; i < n; i++) cin >> p[i].x >> p[i].y;

        double min_len = INF;
        double x1, y1, x2, y2;

        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (i == j) continue;

                double len = getlen(p[i], p[j]);

                if (len < min_len)
                {
                    min_len = len;
                    x1 = p[i].x;
                    y1 = p[i].y;
                    x2 = p[j].x;
                    y2 = p[j].y;
                }
            }
        }

        printf("%.3lf (%.3lf, %.3lf) (%.3lf, %.3lf)\n", min_len, x1, y1, x2, y2);

    }

    return 0;
}

测试数据

/*
input1
6
1 3
2 1
4 1
4 3
3 4
6 2

output1
1.414 (4.000, 3.000) (3.000, 4.000)

input2
14
30 30
50 60
60 20
70 45
86 39
112 60
200 113
250 50
300 200
130 240
76 150
47 76
36 40
33 35

ouput2
5.831 (30.000, 30.000) (33.000, 35.000)
*/

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水能zai舟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值