UVA 11227 The silver bullet.(简单题:枚举经过最多的点的直线)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2168
题意:
平面上有n个点,问你一条直线最多能经过多少个点?
分析:
首先对点集去重,然后直接枚举两个点,然后用该点的直线去判断,看看该直线与多少个点相交即可.
AC代码:
#include<cstdio> #include<cmath> #include<algorithm> using namespace std; const int maxn=100+5; const double eps=1e-10; int dcmp(double x) { if(fabs(x)<eps) return 0; return x<0?-1:1; } struct Point { double x,y; Point(){} Point(double x,double y):x(x),y(y){} bool operator==(const Point &rhs)const { return dcmp(x-rhs.x)==0 && dcmp(y-rhs.y)==0; } bool operator<(const Point &rhs)const { return x<rhs.x ||( dcmp(x-rhs.x)==0 && y<rhs.y); } }P[maxn]; typedef Point Vector; Vector operator-(Point A,Point B) { return Vector(A.x-B.x,A.y-B.y); } double Cross(Vector A,Vector B) { return A.x*B.y-A.y*B.x; } bool InLine(Point P,Point A,Point B)//判断点P在直线AB上 { return dcmp(Cross(P-A,A-B))==0; } int main() { int T; scanf("%d",&T); for(int kase=1;kase<=T;++kase) { int n; scanf("%d",&n); for(int i=0;i<n;++i) scanf("%lf%lf",&P[i].x,&P[i].y); sort(P,P+n); n=unique(P,P+n)-P; if(n==1) { printf("Data set #%d contains a single gnu.\n",kase); continue; } int ans=0; for(int i=0;i<n;++i) for(int j=i+1;j<n;++j) { int num=0; for(int k=0;k<n;++k) if(InLine(P[k],P[i],P[j])) ++num; ans=max(ans,num); } printf("Data set #%d contains %d gnus, out of which a maximum of %d are aligned.\n",kase,n,ans); } return 0; }