求平面n个点间的最大距离 c语言数组

 我来答
如此甚好118
2015-12-09 · TA获得超过117个赞
知道小有建树答主
回答量:92
采纳率:0%
帮助的人:84.9万
展开全部

具体代码如下(仔细看下应该能明白):

/*输入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);
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式