蛮力法 最近对

时间:2018-05-03 09:01:41
【文件属性】:
文件名称:蛮力法 最近对
文件大小:2KB
文件格式:CPP
更新时间:2018-05-03 09:01:41
蛮力法 最近对 //蛮力法 --最近对问题 #include #include #define random(x) (rand()%x) typedef struct node//定义点的结构 { int x; int y; }node; typedef struct nlist//定义点的一个集合链表存储 { struct node data; struct nlist *next; }nlist; typedef struct close//用于保留最近的两点 { node a; node b; double space; }close; //生成节点 nlist *creatnode(int m) { nlist *head,*p,*q; head=(nlist*)malloc(sizeof(nlist)); head->next=NULL; p=head; q=p; int a; printf("生成%d个节点的坐标是:\n",m); for(a=0;adata.x=random(50); p->data.y=random(50); printf("<%d,%d>",p->data.x,p->data.y); q->next=NULL; p->next=q; p=q; } return head; } //蛮力法求最近对 void BruteForce(nlist*head,int m) { int i; int space; close n; nlist *p,*q; p=head; q=p->next; for(i=1;inext) { space=((p->data.x-q->data.x)*(p->data.x-q->data.x)+(p->data.y-q->data.y)*(p->data.y-q->data.y)); getchar(); if(space<20000) { n.a=p->data; n.b=q->data; n.space=space; } q=q->next; } p=p->next; } printf("\n最近的两个点的坐标是:<%d,%d>,<%d,%d>",n.a.x,n.a.y,n.b.x,n.b.y); printf("最近点的距离的平方是:%f",n.space); } int main() { int m; nlist *head; printf("输入创建的节点数"); scanf("%d",&m); head=creatnode(m); BruteForce(head,m); getchar(); }

网友评论