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 +
'}';
}
}
【蓝桥杯】直线(Java)
于 2022-04-01 15:29:51 首次发布