最小套圈问题双重循环算法

/*   Copyright    by ZhongMing-Bian     Jan,6,2010     */
/*       最小套圈问题双重循环算法   低效率  遍历        */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define  MAX_POINTS  100000    /*坐标点数的最大值 */
#define MAX_TEST 100     /*最大测试次数*/

typedef  struct  {   /*定义坐标点*/
    float  x;
    float  y;
}  Point;

float  dist(Point  a,Point  b)  {       /*求两个坐标点之间的距离*/
    return  sqrt((float)(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
  }

float  nearest(Point* points,  int  n)  {    /*求最小距离,双循环遍历*/
    float near=100000,temp;
    int i,j;
    if(n==1) return 0;
    else{
      for(i=0; i<n; i++)
          for(j=0; j<n; j++){
              if(i==j)  continue;
              else {
                temp=dist(points[i],points[j]);
                near=(near>temp)?temp:near;
              }
           
          }
    return near;
  }
}

void main() {
    int i,j=0,numPoints;
    Point  points[MAX_POINTS];
    float result[MAX_TEST];        /*记录结果*/
    for(i=0;i<MAX_TEST;i++)    result[i]=-1;
    printf("请输入数据:/n");
    while(1){                     /*进行多次测试*/
loop:   scanf("%d",  &numPoints);
        if(!numPoints) break;
        if(numPoints<0||numPoints>MAX_POINTS) {
            printf("Error!/n");        /*容错处理*/
            goto loop;
        }
        for(i=0;  i<numPoints;  i++)    /*输入点的数量及点的坐标值*/
            scanf("%f  %f",  &points[i].x,  &points[i].y);
        result[j]=nearest(points, numPoints)/2;
        j++;
        }
    i=0;
    printf("/n最短距离分别为:/n");
    while(result[i]!=-1)    /*输出测试结果*/
            printf("%.2f/n",result[i++]);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值