其他分享
首页 > 其他分享> > POJ 1118 Lining Up

POJ 1118 Lining Up

作者:互联网

/*
【题目】POJ 1118 
【题意】判断多个点中最多有多少个点共线
【题解】按横纵坐标大小排序,从小到大遍历每一个点,
计算该点与剩余点的斜率,记录到数组中
在数组中查找有有多少个斜率相同的点
注意只有两个点的情况,应该输出2 

*/

#include<iostream>
#include<vector>
#include<cstdio> 
#include<algorithm>
#include<cmath>
using namespace std;
const int inf=0x3f3f3f;
struct Point{
	double x,y;
	Point(double x=0,double y=0):x(x),y(y){}
};
Point a[1000];

typedef Point Vector;
Vector operator + (Vector A,Vector B){
	return Vector(A.x+B.x,A.y+B.y);
} 
Vector operator - (Vector A,Vector B){
	return Vector(A.x-B.x,A.y-B.y);
} 
Vector operator * (Vector A,double p){
	return Vector(A.x*p,A.y*p);
} 
Vector operator / (Vector A,double p){
	return Vector(A.x/p,A.y/p);
} 

bool operator < (const Point &a,const Point &b){
	if(a.x==b.x){
		return a.y<b.y;
	}
	return a.x<b.x;
}
const double eps=1e-6;
int sgn(double x){
	if(fabs(x)<eps){
		return 0;
	}
	if(x<0){
		return -1;
	}
	return 1;
}

bool operator == (const Point &a,const Point &b){
	if(sgn(a.x-b.x)==0&&sgn(a.y-b.y)==0){
		return true;
	}
	return false;
}
double pos[1000];
int main(){
	while(1){
		int n;
		cin>>n;
		if(n==0){
			break;
		}
		for(int i=0;i<n;i++){
			cin>>a[i].x>>a[i].y; 
		}
		sort(a,a+n);
		int ans=1;
		for(int i=0;i<n;i++){
			int cur=0;
			for(int j=i+1;j<n;j++){
				if(a[j].x-a[i].x==0){
					pos[cur++]=inf;
				}
				else{
					pos[cur++]=(a[j].y-a[i].y)/(a[j].x-a[i].x); 
				}
			}
			sort(pos,pos+cur);
			int cnt=1;
			for(int j=1;j<cur;j++){
				if(fabs(pos[j]-pos[j-1])<1e-6){
					cnt++;
					ans=max(ans,cnt);
				}else{
					cnt=1;
				}
			}
		}
		cout<<ans+1<<endl;
	}
	return 0;
} 

 

标签:return,Point,double,Up,Vector,POJ,operator,1118,include
来源: https://blog.csdn.net/Helloirbd/article/details/101198861