最近点对问题用c语言实现

  • Post author:
  • Post category:其他


#include<stdio.h>

#include<math.h>

#include<stdlib.h>

#define MAX 100

struct {

int x,y;

}zuobiao[MAX];

double zuijin()

{

int i,j,t;

int d=100;

int n=rand()%100+1;

printf(“随机生成的点数为: %d\n”,n);

for(i=0;i<n;i++){

zuobiao[i].x=rand()%100+1;

zuobiao[i].y=rand()%100+1;

printf(“(%d,%d)     “,zuobiao[i].x,zuobiao[i].y);

}

for (i = 0; i <n+1; i++)

for (j=i+1; j < n; j++) {

t = (zuobiao[i].x-zuobiao[j].x)*(zuobiao[i].x-zuobiao[j].x)+(zuobiao[i].y-zuobiao[j].y)*(zuobiao[i].y-zuobiao[j].y);

if(d*d>t)d=t;

}

return sqrt(d);

}

void main() {

printf(“\n最近的距离为:%lf\n”,zuijin());

}



版权声明:本文为attack_5原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。