O(n^3)的做法:
枚举任意两点为弦的圆,然后再枚举其它点是否在圆内。
用到了两个函数
atan2反正切函数,据说可以很好的避免一些特殊情况
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 #include<cmath> 8 #include<queue> 9 #include<set> 10 using namespace std; 11 #define N 310 12 #define LL long long 13 #define INF 0xfffffff 14 const double eps = 1e-8; 15 const double pi = acos(-1.0); 16 const double inf = ~0u>>2; 17 struct point 18 { 19 double x,y; 20 point(double x = 0,double y =0 ):x(x),y(y){} 21 }p[N]; 22 double dis(point a,point b) 23 { 24 return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); 25 } 26 point getcircle(point p1,point p2) 27 { 28 point mid = point((p1.x+p2.x)/2,(p2.y+p1.y)/2); 29 double angle = atan2(p2.y-p1.y,p2.x-p1.x); 30 double d = sqrt(1.0-dis(p1,mid)*dis(p1,mid)); 31 return point(mid.x+d*sin(angle),mid.y-d*cos(angle)); 32 } 33 int dcmp(double x) 34 { 35 if(fabs(x)<eps)return 0; 36 else return x<0?-1:1; 37 } 38 int main() 39 { 40 int i,j,n; 41 while(scanf("%d",&n)&&n) 42 { 43 for(i = 1 ;i <= n; i++) 44 scanf("%lf%lf",&p[i].x,&p[i].y); 45 int maxz = 1; 46 for(i = 1; i <= n; i++) 47 for(j = i+1 ; j <= n ;j++) 48 { 49 if(dis(p[i],p[j])>2.0) continue; 50 int tmax = 0; 51 point cir = getcircle(p[i],p[j]); 52 for(int g = 1; g <= n ;g++) 53 { 54 if(dcmp(dis(cir,p[g])-1.0)>0) 55 continue; 56 tmax++; 57 } 58 maxz = max(maxz,tmax); 59 } 60 printf("%d\n",maxz); 61 } 62 return 0; 63 }