【蓝桥杯】直线(Java)

import java.util.HashSet;
import java.util.Objects;

public class Main_06 {
    /**
     * 直线
     * 在平面直角坐标系中,两点可以确定一条直线。如果有多点在一条直线上, 那么这些点中任意两点确定的直线是同一条。
     * 正确答案:40257           我的:40524 
     * 修改:
     * 注意:k d 为double类型
     *      +20
     */
    public static void main(String[] args) {
        HashSet<line> lines = new HashSet<>();
        for (int x1 = 1; x1 <= 19; x1++) {
            for (int y1 = 0; y1 <= 20; y1++) {//x1 y1
                for (int x2 = 0; x2 <=x1-1; x2++) {
                    for (int y2 = 0; y2 <=20 ; y2++) {//x2 y2
                          line l1= new line(x1,y1,x2,y2);
                          l1.k_d();
                          //System.out.println(l1);
                          //System.out.println("x1="+x1+"\t"+"y1="+y1+"\t"+"x2="+"\t"+x2+"y2="+y2+"\t");
                          lines.add(l1);
                    }
                }


            }
        }

        System.out.println(lines.size()+20);

    }
}
class line{
    public double k;
    public double d;
    public int x1,y1,x2,y2;
    //k1==k2&&d1==d2 两条直线相等

    public line(int x1, int y1, int x2, int y2) {
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
    }
    //求斜率 截距
    public void k_d(){
        if (this.x1!=this.x2){
            this.k=(double) ((double)(this.y2-this.y1)/(double)(this.x2-this.x1));
            this.d=(double)((double)(this.x2*this.y1-this.x1*this.y2)/(double)(this.x2-this.x1));
        }
        else if (this.y1==this.y2){
            this.k=0;
            this.d=this.y1;
        }
        else{
            this.k=999999999;
            this.d=x1;
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        line line = (line) o;
        return k == line.k && d == line.d;
    }
    @Override
    public int hashCode() {
        return Objects.hash(k, d);
    }

    @Override
    public String toString() {
        return "line{" +
                "k=" + k +
                ", d=" + d +
                '}';
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值