题意:给定原点及n个点,找到一个圆使得尽可能多的点在圆上
题解:三点可以确定一个圆,原点固定,遍历两个点去确定圆心,并用map保存圆心,当再次得到一个相同的圆心时,map++(圆心相同,且有共点必定为同一个圆)
#include<iostream> #include<stdio.h> #include<math.h> #include<map> using namespace std; const int maxn = 2e3+10; const double eps=1e-5; struct Point{ double x,y; }a[maxn]; map<pair<double,double>,int> mp; int main(){ int n; cin>>n; for(int i=1;i<=n;i++){ scanf("%lf %lf",&a[i].x,&a[i].y); } int ans = 0; for(int i=1;i<=n;i++){ mp.clear(); for(int j=i+1;j<=n;j++){ double x1 = a[i].x,x2 = a[j].x,y1 = a[i].y,y2 = a[j].y; double x3=0,y3=0; double a = x1-x2; double b = y1-y2; double c = x1-x3; double d = y1-y3; double e = ((x1*x1-x2*x2)+(y1*y1-y2*y2))/2.0; double f = ((x1*x1-x3*x3)+(y1*y1-y3*y3))/2.0; double det = b*c-a*d; if(fabs(det)<eps){ continue; } double x = -(d*e-b*f)/det; double y = -(a*f-c*e)/det; ans = max(ans,++mp[{x,y}]); } } cout<<ans+1<<endl;//自己这个点要算进去 return 0; }
三点确定圆心模板:
#include <bits/stdc++.h> using namespace std; #define db double #define pdd pair<db, db> const db eps = 1e-5; pdd Circle_center(db x1, db x2, db x3, db y1, db y2, db y3) { db a = x1 - x2; db b = y1 - y2; db c = x1 - x3; db d = y1 - y3; db e = ((x1 * x1 - x2 * x2) + (y1 * y1 - y2 * y2)) / 2.0; db f = ((x1 * x1 - x3 * x3) + (y1 * y1 - y3 * y3)) / 2.0; db det = b * c - a * d; if (fabs(det) < eps) //三点共线 return {0, 0}; db x = -(d * e - b * f) / det; db y = -(a * f - c * e) / det; return {x, y}; } int main(void) {}