(c语⾔)⽤蛮⼒法解决最近对问题
c语⾔⽤蛮⼒法解决最近对问题
#include<stdio.h>
明解c语言#include<math.h>
#include<time.h>
#define MAX 1000
#define M 100
typedef struct Point
{
int x;
int y;
}P[M];
double ClosePoint(P p, int n, int& ind1, int& ind2)
{
double dmin = MAX, d;
for (int i = 0;i < n;i++)
{
for (int j = i + 1;j < n;j++)
{
d = (p[i].x - p[j].x) * (p[i].x - p[j].x) + (p[i].y - p[j].y) * (p[i].y - p[j].y);
if (d < dmin)
{
dmin = d;
ind1 = i;
ind2 = j;
}
}
}
return dmin;
}
int main()
{
int ind1, ind2, n;
P p;
double dmin;
printf(“请输⼊点的总个数”);
scanf_s("%d",&n);
for (int i = 0;i < n;i++)
{
printf(“P%d “,i+1);
scanf_s(”%d”,&p[i].x);
scanf_s("%d",&p[i].y);
}
dmin = sqrt(ClosePoint(p, n, ind1, ind2));
/
/printf("%lf", sqrt(ClosePoint(p, n, ind1, ind2)));
printf(“最近的两个点是”);
printf(“p%d”, ind1 + 1);
printf(“p%d”, ind2 + 1);
printf(“最近的距离是:%lf”, dmin);
return 0;
}
例⼦
5 10
9 1
10 12 10 19 13 15
15 19
16 20 19 9 24 0 24 12
31 32
32 24
38 29 40 56 45 23

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。