c语言课程设计的题目,求大神帮忙打一下谢谢!要求在下面图

2025-02-23 23:05:41
推荐回答(1个)
回答1:

代码较长,麻烦较多,调试费时;若正确且帮助了你,请采纳。

代码文本:

#include "stdio.h"

#include

#include "math.h"

#include "time.h"

typedef struct B{

double x,y;

}PB;

void B_point(PB *p,int n){

double x,y;

int i,j;

srand((unsigned)time(NULL));

for(i=0;i

x=rand()%200/3.0;

y=rand()%200/3.0;

for(j=0;j

if(p[j].x==x && p[j].y==y)

break;

if(j>=i)

p[i].x=x,p[i].y=y;

else

i--;

}

}

double dist(PB p1,PB p2){

return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));

}

double short_dist(PB *p,int n,int *s1,int *s2){

int i,j;

double m,t;

m=dist(p[*s1=0],p[*s2=1]);

for(i=0;i

for(j=i+1;j

if(m>(t=dist(p[i],p[j])))

m=t,*s1=i,*s2=j;

return m;

}

int main(int argc,char *argv[]){ 

PB *p;

int n,i,j;

double s;

printf("Enter n(int 0

if(scanf("%d",&n)!=1 || n<1){

printf("Input error, exit...\n");

return 0;

}

if((p=(PB *)malloc(sizeof(PB)*n))==NULL){

printf("Application memory failure, exit...\n");

return 0;

}

B_point(p,n);//生成楼宇座标

printf("\n%d buildings coordinates is as follows:\n",n);

for(i=0;i

printf("B%d(%.2f,%.2f)\n",i+1,p[i].x,p[i].y);

s=short_dist(p,n,&i,&j);

printf("The nearest 2 buildings is B%d <--> B%d, distance is %.2f\n",i+1,j+1,s);

free(p);

return 0;

}