//开始想到是凸包上的点。。其实不用直接枚举就好了。。但是我的算法实在是烂,,4000ms+。。有过跟没过一样。。
//这道只要想到圆就差不多了。。
nclude <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#define pi acos(-1.0)
using namespace std;
typedef double pointper;//点坐标的类型
#define POINTNUM 1005//最多点的个数
#define PREX 1e-11 //当点坐标为实数型的时候用
struct node
{
pointper x,y;
}Point[1005];
node ans;
class Yuan
{
public:
node point[POINTNUM];
int n;
double dis(node a,node b)
{
return sqrt((double)((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)));
}
double Max(double a, double b, double c)
{
double max;
max=a>b?a:b;
max=max>c?max:c;
return max;
}
node cir(node a,node b,node c) //这个已知3点求圆心的算法要记得。。
{
node ret;
double a1 = b.x - a.x, b1 = b.y - a.y, c1 = (a1*a1 + b1*b1)/2;
double a2 = c.x - a.x, b2 = c.y - a.y, c2 = (a2*a2 + b2*b2)/2;
double d = a1 * b2 - a2 * b1;
if(d!=0)
{
ret.x = a.x + (c1*b2 - c2*b1)/d;
ret.y = a.y + (a1*c2 - a2*c1)/d;
}
return ret;
}
};
Yuan yuan;
int main()
{
int X,Y,i,j,k;
while(scanf("%d%d%d",&X,&Y,&yuan.n)!=EOF)
{
for(i=0;i<yuan.n;i++)
{
cin>>yuan.point[i].x>>yuan.point[i].y;
}
if(yuan.n==1)
{
printf("(%.1lf,%.1lf).\n",yuan.point[0].x,yuan.point[0].y);
printf("0.0\n");//这里是第一个要注意的地方。。当n=1,n=2的时候要特殊处理
}
else
if(yuan.n==2)
{
printf("(%.1lf,%.1lf).\n",(yuan.point[0].x+yuan.point[1].x)/2,(yuan.point[0].y+yuan.point[1].y)/2);
printf("%.1lf",yuan.dis(yuan.point[0],yuan.point[1]));
}
else
{
// yuan.pointselect(1);
double a,b,c,p,f,sum=0;
double r;
double temp=0;
double max;
for(i=0;i<yuan.n;i++)
for( j=i+1;j<yuan.n;j++)
for( k=j+1;k<yuan.n;k++)
{
a=yuan.dis(yuan.point[i],yuan.point[j]);
b=yuan.dis(yuan.point[i],yuan.point[k]);
c=yuan.dis(yuan.point[j],yuan.point[k]);
p=(a+b+c)/2.0;
f=sqrt(p*(p-a)*(p-b)*(p-c));
max=yuan.Max(a,b,c);
if(a==max)
{
a=c;
c=max;
}
else if(b==max)
{
b=c;
c=max;
}
if(a*a+b*b<c*c)
{
r=max/2;
}
else
{
r=(double)(a*b*c)/(double)(4*f);
}
if(r>temp)
{
temp=r;
ans=yuan.cir(yuan.point[i],yuan.point[j],yuan.point[k]);
}
}
printf("(%.1lf,%.1lf).\n",ans.x,ans.y);
printf("%.1lf\n",temp);
}
}
return 0;
}
由于我的实在是烂。。贴个好的。。
#include <stdio.h>
#include <math.h>
const int maxn = 1005;
//const double eps = 1e-6;
struct TPoint
{
double x, y;
TPoint operator-(TPoint &a)
{
TPoint p1;
p1.x = x - a.x;
p1.y = y - a.y;
return p1;
}
};
struct TCircle
{
double r;
TPoint centre;
};
struct TTriangle
{
TPoint t[3];
};
TCircle c;
TPoint a[maxn];
double distance(TPoint p1, TPoint p2)
{
TPoint p3;
p3.x = p2.x - p1.x;
p3.y = p2.y - p1.y;
return sqrt(p3.x * p3.x + p3.y * p3.y);
}
double triangleArea(TTriangle t)
{
TPoint p1, p2;
p1 = t.t[1] - t.t[0];
p2 = t.t[2] - t.t[0];
return fabs(p1.x * p2.y - p1.y * p2.x) / 2;
}
TCircle circumcircleOfTriangle(TTriangle t)
{
//三角形的外接圆
TCircle tmp;
double a, b, c, c1, c2;
double xA, yA, xB, yB, xC, yC;
a = distance(t.t[0], t.t[1]);
b = distance(t.t[1], t.t[2]);
c = distance(t.t[2], t.t[0]);
//根据S = a * b * c / R / 4;求半径R
tmp.r = a * b * c / triangleArea(t) / 4;
xA = t.t[0].x; yA = t.t[0].y;
xB = t.t[1].x; yB = t.t[1].y;
xC = t.t[2].x; yC = t.t[2].y;
c1 = (xA * xA + yA * yA - xB * xB - yB * yB) / 2;
c2 = (xA * xA + yA * yA - xC * xC - yC * yC) / 2;
tmp.centre.x = (c1 * (yA - yC) - c2 * (yA - yB)) /
((xA - xB) * (yA - yC) - (xA - xC) * (yA - yB));
tmp.centre.y = (c1 * (xA - xC) - c2 * (xA - xB)) /
((yA - yB) * (xA - xC) - (yA - yC) * (xA - xB));
return tmp;
}
TCircle MinCircle2(int tce, TTriangle ce)
{
TCircle tmp;
if(tce == 0) tmp.r = -2;
else if(tce == 1)
{
tmp.centre = ce.t[0];
tmp.r = 0;
}
else if(tce == 2)
{
tmp.r = distance(ce.t[0], ce.t[1]) / 2;
tmp.centre.x = (ce.t[0].x + ce.t[1].x) / 2;
tmp.centre.y = (ce.t[0].y + ce.t[1].y) / 2;
}
else if(tce == 3) tmp = circumcircleOfTriangle(ce);
return tmp;
}
void MinCircle(int t, int tce, TTriangle ce)
{
int i, j;
TPoint tmp;
c = MinCircle2(tce, ce);
if(tce == 3) return;
for(i = 1;i <= t;i++)
{
if(distance(a[i], c.centre) > c.r)
{
ce.t[tce] = a[i];
MinCircle(i - 1, tce + 1, ce);
tmp = a[i];
for(j = i;j >= 2;j--)
{
a[j] = a[j - 1];
}
a[1] = tmp;
}
}
}
void run(int n)
{
TTriangle ce;
MinCircle(n, 0, ce);
printf("(%.1lf,%.1lf).\n%.1lf\n", c.centre.x, c.centre.y, c.r);
}
int main()
{
int X,Y,n;
while(scanf("%d%d%d", &X,&Y,&n) != EOF)
{
for(int i = 1;i <= n;i++)
scanf("%lf%lf", &a[i].x, &a[i].y);
run(n);
}
return 0;
} */