7.6--找过点最多的直线(CC150)

直接两个点确定一条直线。然后两两组合,再写一个看过多少个点的函数。一直更新max就行。

 
  
import java.util.Arrays;

public class Solution {

    public static void main(String[] args){
        Point[] a = {new Point(0,0),new Point(1,1),new Point(2,2),new Point(0,3)};

        System.out.println(Arrays.toString(getLine(a,4)));
    }
    public static  double[] getLine(Point[] p, int n){
        double[] resLast = new double[2];
        //思路:两个点得到一条直线。
        int max = 0;
        for(int i = 0; i < p.length; i++){
            for(int j = i + 1; j < p.length; j++){
                double[] res = new double[2];
                //把直线求出来。并算出个数
                int num = 0;
                res[0] = (p[j].y - p[i].y) * 1.0 / (p[j].x - p[i].x);
                res[1] = p[j].y - res[0] *p[j].x;
                num = num(p,res    );
                if(num > max){
                    max = num;
                    resLast = res;
                }
            }
        }

        return resLast;
    }

    public static int num(Point[] p, double[] res){
        int num = 0;
        for(int i = 0; i < p.length; i++){
            if(p[i].y == (p[i].x * res[0] + res[1])){
                num++;
            }
        }

        return num;
    }


}

class Point {
    int x;
    int y;
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
    public Point() {
        this.x = 0;
        this.y = 0;
    }
}
 
  

 

 

 

转载于:https://www.cnblogs.com/yueyebigdata/p/5090072.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值