用c语言代码
时间: 2023-10-18 13:04:17 浏览: 55
好的,以下是 C 语言的代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAXN 100000
#define INF 1000000000
typedef struct {
double x, y;
} Point;
Point a[MAXN];
Point t[MAXN];
int cmp(const void *p1, const void *p2) {
Point *a = (Point *)p1;
Point *b = (Point *)p2;
if (a->x != b->x) return a->x < b->x ? -1 : 1;
return a->y < b->y ? -1 : 1;
}
double dist(Point a, Point b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
double solve(int l, int r) {
if (l == r) return INF;
if (l + 1 == r) return dist(a[l], a[r]);
int mid = (l + r) / 2;
double d = fmin(solve(l, mid), solve(mid + 1, r));
int i, j, k = 0;
for (i = l; i <= r; i++) {
if (fabs(a[i].x - a[mid].x) <= d) {
t[k++] = a[i];
}
}
qsort(t, k, sizeof(Point), cmp);
for (i = 0; i < k; i++) {
for (j = i + 1; j < k && t[j].y - t[i].y <= d; j++) {
double d2 = dist(t[i], t[j]);
if (d > d2) d = d2;
}
}
return d;
}
int main() {
int n, i;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%lf%lf", &a[i].x, &a[i].y);
}
qsort(a, n, sizeof(Point), cmp);
double d = solve(0, n - 1);
printf("(%.2f,%.2f),(%.2f,%.2f),miniDist=%.3f\n",
a[0].x, a[0].y, a[1].x, a[1].y, d);
return 0;
}
```
具体实现与思路与 Python 版本基本一致,只是用了 C 语言的特性。
阅读全文