
求平面n个点间的最大距离 c语言数组
1个回答
展开全部
具体代码如下(仔细看下应该能明白):
/*输入n个点,求平面上所有各点之间的最长距离,要求定义和调用函数计算距离*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
typedef struct point
{
double x;
double y;
}point;
double calculate_distance(point* a, point* b)
{
double i = a->x - b->x;
double j = a->y - b->y;
double k = pow(i, 2) + pow(j, 2);
return sqrt(k);
}
void main()
{
int i = 0;
int j = 0;
int n = 0;
double distance = 0;
double max_distance = 0;
printf("Enter n:");
scanf("%d", &n);
point* store_point = NULL;
store_point = (point*)malloc(n*sizeof(point));
point* i_point = store_point;
printf("input x, y\n");
for (i=0; i<n; i++)
{
scanf("%lf %lf", &(i_point->x), &(i_point->y));
i_point++;
}
i_point = store_point;
for (i=0; i<n; i++)
{
for (j=1; j<n; j++)
{
distance = calculate_distance((i_point+i), (i_point+j));
if (distance > max_distance)
{
max_distance = distance;
}
}
}
free(store_point);
store_point = i_point = NULL;
printf("Max Distance = %lf\n", max_distance);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询