您的位置:首页 > 其它

hdu 1007

2013-11-29 00:00 134 查看
设计最小的套环半径,令x,y分别从小到大排序,如果前一个X或前一个Y加上当前半径要大于后一个x或y则可以肯定这个半径必能一下子套中两个。所以要计算新的半径。

code:

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include "iostream" struct Point {     double x,y; }; Point p[100005];

int cmps(Point a,Point b) //x从小到大 {  if(a.x<b.x)  {   return 1;  }else if(a.x==b.x&&a.y<b.y)  {   return 1;  }else  return 0; }

int cmps2(Point a,Point b) //y从小到大 {  if(a.y<b.y)  {   return 1;  }else if(a.y==b.y&&a.x<b.x)  {   return 1;  }else  return 0; } using namespace std; int main() {     int n,i;     double s,tmp;     while(scanf("%d",&n)!=EOF&&n)     {         for(i=0;i<n;i++)         {             scanf("%lf%lf",&p[i].x,&p[i].y);             if(p[i].x>0&&p[i].y<0)             {              p[i].x*=-1;  //第四像限的全弄到第三像限来             }         }        // qsort(p,n,sizeof(Point),cmp1);        sort(p,p+n,cmps);         for(i=1,s=111111111;i<n;i++)         {    if(p[i-1].x+s>p[i].x){     tmp=sqrt(pow(p[i].x-p[i-1].x,2)+pow(p[i].y-p[i-1].y,2))/2;             if(s>tmp)             {                 s=tmp;             } }         }     //qsort(p,n,sizeof(Point),cmp1);     sort(p,p+n,cmps2);         for(i=1;i<n;i++)         { if(p[i-1].y+s>p[i].y){ tmp=sqrt(pow(p[i].x-p[i-1].x,2)+pow(p[i].y-p[i-1].y,2))/2;             if(s>tmp)             {                 s=tmp;             }}         }         printf("%.2lf\n",s);     }     return 0; }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: