Chinaunix首页 | 论坛 | 博客
  • 博客访问: 255926
  • 博文数量: 170
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1709
  • 用 户 组: 普通用户
  • 注册时间: 2014-05-06 18:01
文章分类

全部博文(170)

文章存档

2016年(11)

2015年(130)

2014年(29)

分类: Java

2015-03-22 16:56:29

//Given n points on a 2D plane, find the maximum number of points that lie on the same 


straight line.
class Point {
    int x;
    int y;
    Point() { x = 0; y = 0; }
    Point(int a, int b) { x = a; y = b; }
 }
public class MaxPoints {
public static void main(String[] arg){
Point a=new Point(1,1);
Point b=new Point(1,1);
Point c=new Point(2,2);
Point d=new Point(2,2);
   Point[] points=new Point[4];
   points[0]=a;
   points[1]=b;
   points[2]=c;
   points[3]=d;
System.out.print(maxPoints(points));
}
public static int maxPoints(Point[] points) {
if(points.length<=2){
return points.length;
}
int i,j,k;
int maxnumber=0;  
    for(i=0;i     for(j=i+1;j     int count=0;
    if(points[i].x-points[j].x==0){
   
        for(k=0;k        if(k!=i&&k!=j)
    if(points[i].x-points[k].x==0)
    count++;
       
   
    }
    else{
    for(k=0;k     if(k!=i&&k!=j){
    if(((points[i].x-points[k].x)*(points[i].y-points


[j].y))==((points[i].x-points[j].x)*(points[i].y-points[k].y)))
    count++;
    }
   
    }
   
    }
    if(count>maxnumber){
    maxnumber=count;
    }
  
    }
    }
    
    return maxnumber+2;
 }
}


阅读(436) | 评论(0) | 转发(0) |
0

上一篇:MaximumProductSubarray

下一篇:PalindromeNumber

给主人留下些什么吧!~~