题意:给出n个点,求所有通过原点的圆中,经过给出点最多的圆,然后输出点的最大个数。
题解:同一圆上点的个数
绕了半天,其实只要用map存储有同一圆心的点有几个就行。
还要注意的是若任意两点与原点构不成圆,答案是1。
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<fstream>
#include<set>
#include<map>
#include<sstream>
#include<iomanip>
#define ll long long
using namespace std;
//二维计算几何模板
const double eps = 1e-8;
const double pi = acos(-1.0);
const int maxp = 1010;
//Compares a double to zero
int sgn(double x) {
if (fabs(x) < eps) return 0;
if (x < 0) return -1;
else return 1;
}
//POINT
struct Point {
double x, y;
Point() {}
Point(double _x, double _y) {
x = _x;
y = _y;
}
Point operator -(const Point& b)const {
return Point(x - b.x, y - b.y);
}
//叉积
double operator ^(const Point& b)const {
return x * b.y - y * b.x;
}
//返回两点的距离
double distance(Point p) {
return hypot(x - p.x, y - p.y);
}
Point operator +(const Point& b)const {
return Point(x + b.x, y + b.y);
}
Point operator /(const double& k)const {
return Point(x / k, y / k);
}
//逆时针旋转 90 度
Point rotleft() {
return Point(-y, x);
}
};
//LINE
struct Line {
Point s, e;
Line() {}
Line(Point _s, Point _e) {
s = _s;
e = _e;
}
//求两直线的交点
//要保证两直线不平行或重合
Point crosspoint(Line v) {
double a1 = (v.e - v.s) ^ (s - v.s);
double a2 = (v.e - v.s) ^ (e - v.s);
return Point((s.x * a2 - e.x * a1) / (a2 - a1), (s.y * a2 - e.y * a1) / (a2 - a1));
}
};
//圆
struct circle {
Point p;//圆心
double r;//半径
circle() {}
//三角形的外接圆
//需要 Point 的 + / rotate() 以及 Line 的 crosspoint()
//利用两条边的中垂线得到圆心
//测试: UVA12304
circle(Point a, Point b, Point c) {
Line u = Line((a + b) / 2, ((a + b) / 2) + ((b - a).rotleft()));
Line v = Line((b + c) / 2, ((b + c) / 2) + ((c - b).rotleft()));
p = u.crosspoint(v);
r = p.distance(a);
}
};
int n;
double x[2222], y[2222];
map<Point, int> ma;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lf%lf", &x[i], &y[i]);
Point origin = Point(0, 0);
int ans = 1;
for (int i = 1; i <= n; i++) {
ma.clear();
for (int j = i + 1; j <= n; j++) {
if (x[i] * y[j] == x[j] * y[i]) continue;
int temp = 0;
circle c = circle(origin, Point(x[i], y[i]), Point(x[j], y[j]));
if (c.p.x == INFINITY) continue;
ma[c.p]++;
ans = max(ans, ma[c.p] + 1);
}
}
printf("%d\n", ans);
return 0;
}