http://www.lintcode.com/zh-cn/problem/max-points-on-a-line/
/**
* Definition for a point.
* struct Point {
* int x;
* int y;
* Point() : x(0), y(0) {}
* Point(int a, int b) : x(a), y(b) {}
* };
*/
class Solution {
public:
/**
* @param points an array of point
* @return an integer
*/
int maxPoints(vector<Point>& points) {
// Write your code here
int maxc = 0;
if (points.size()>2)
for (int i = 0; i < points.size() - 2; i++){
for (int z = i+1; z < points.size() ; z++){
double x, c;
if (points[z].x - points[i].x == 0){//x坐标上的垂直线
int ret = 2;
for (int j = i + 1; j < points.size(); j++)
{
if (j == z)continue;
if (points[j].x == points[i].x){
ret++;
}
}
maxc = max(maxc, ret);
}
else if (points[z].y - points[i].y == 0){//x坐标上的平行线
int ret = 2;
for (int j = i + 1; j < points.size(); j++)
{
if (j == z)continue;
if (points[j].y == points[i].y){
ret++;
}
}
maxc = max(maxc, ret);
}
else{// 1 3 2 6
x = (points[z].y - points[i].y) / (double)(points[z].x - points[i].x);
c = points[i].y - points[i].x*x;
int ret = 2;
for (int j = i + 1; j < points.size(); j++)
{
if (j == z)continue;
if (fabs(points[j].x*x + c - points[j].y)<1e-6 ){
ret++;
}
}
maxc = max(maxc, ret);
}
}
}
else {
return points.size();
}
return maxc;
}
};