-
题目描述:
-
给定平面上的n个点,任意做一条直线,求至多能有几个点恰好落在直线上。
-
输入:
-
包含多组测试数据,每组测试数据由一个整数n(0<=n<=100)开始,代表平面上点的个数。
接下去n行每行给出一个点的坐标(x,y),x、y的绝对值均小于等于100。
-
输出:
-
对于每组测试数据,输出一个整数,表示至多能有几个点恰好落在直线上。
-
样例输入:
-
2 0 0 1 1 4 0 0 1 1 2 2 3 6
-
样例输出:
-
2 3
代码
#include <iostream> #include <string> #include <string.h> #include <map> #include <stdio.h> #include <algorithm> #include <queue> #include <vector> #include <math.h> #include <set> #define Max(a,b) ((a)>(b)?(a):(b)) #define Min(a,b) ((a)<(b)?(a):(b)) #define SWAP(a , b){ int temp = a; a = b; b = temp;} #define Max_N 108 using namespace std; int N; struct Node//结构体,计算斜率 { float yy;//y1-y2 float xx;//x1-x2 Node(){}; Node(float y,float x):yy(y),xx(x){}; bool friend operator< (const Node A,const Node B) { return A.yy * B.xx < A.xx * B.yy; } bool friend operator== (const Node A,const Node B) { return A.yy * B.xx == A.xx * B.yy; } }; struct Point//平面上的点 { float x; float y; }P[Max_N]; void read() { for(int i = 1;i <= N;i++) //scanf("%f%f",&P[i].x,&P[i].y); cin>>P[i].x>>P[i].y; } int getmax(int id) { map<Node , int> mp;//映射,Node -> int map<Node , int>::iterator it; mp.clear(); int ans = 0; for(int i = 1;i <= N;i++) { if(i == id) continue; if(P[i].x == P[id].x) ans++; else mp[Node(P[id].y-P[i].y , P[id].x-P[i].x)]++; } for(it = mp.begin();it != mp.end();it++) ans = Max(ans,it->second); return ans + 1; } int solve() { read(); int maxnum = 0; for(int i = 1;i <= N;i++) maxnum = Max(maxnum , getmax(i)); return maxnum; } int main() { while(cin >> N) { if(N == 0) {cout <<0<<endl;continue;} cout << solve() <<endl; } return 0; }