输入第一行为样例数m,接下来m行每行6个整数分别表示三个点的横纵坐标。
对于每个样例输出两行,第一行根据是否直角三角形输出Yes或No,第二行输出三角形的周长,保留小数点后两位。
1 0 0 3 0 0 4
Yes 12.00
import java.text.DecimalFormat; import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int m = scanner.nextInt(); for (int i = 0; i < m; i++) { int x1 = scanner.nextInt(); int y1 = scanner.nextInt(); int x2 = scanner.nextInt(); int y2 = scanner.nextInt(); int x3 = scanner.nextInt(); int y3 = scanner.nextInt(); int[] edge = new int[3]; edge[0]= (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2); edge[1]=(x1-x3)*(x1-x3)+(y1-y3)*(y1-y3); edge[2]= (x2-x3)*(x2-x3)+(y2-y3)*(y2-y3); Arrays.sort(edge); DecimalFormat f = new DecimalFormat("0.00"); System.out.println((edge[0]+edge[1])==edge[2]?"Yes":"No"); System.out.println(f.format(Math.sqrt(edge[0])+Math.sqrt(edge[1])+Math.sqrt(edge[2]))); } } }
import java.awt.Point;
import java.util.Scanner;
public class Main {
static class CTriangle {
Point p1 = new Point();
Point p2 = new Point();
Point p3 = new Point();
public CTriangle() {
// TODO Auto-generated constructor stub
}
public boolean rightTriangle() {
double s1 = this.p1.distance(p2);
double s2 = this.p1.distance(p3);
double s3 = this.p2.distance(p3);
if (s1 * s1 + s2 * s2 == s3 * s3) {
return true;
} else if (s1 * s1 + s3 * s3 == s2 * s2) {
return true;
} else if (s3 * s3 + s2 * s2 == s1 * s1) {
return true;
} else {
return false;
}
}
public String Perimeter() {
return String.format("%.2f",this.p1.distance(this.p2) + this.p1.distance(this.p3) + this.p3.distance(this.p2));
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
for (int i = 0; i < m; i++) {
CTriangle ctr = new CTriangle();
int a = sc.nextInt();
int b = sc.nextInt();
ctr.p1.setLocation(a, b);
int c = sc.nextInt();
int d = sc.nextInt();
ctr.p2.setLocation(c, d);
int e = sc.nextInt();
int f = sc.nextInt();
ctr.p3.setLocation(e, f);
if(ctr.rightTriangle()){
System.out.println("Yes");
}else{
System.out.println("No");
}
System.out.println(ctr.Perimeter());
}
}
}
先是用了Java,静态内部类,构造了一个三角形类,里面添加了判断是否是直角和求周长的方法。用了一些Java的包,比如说计算两点之间距离那块儿,还有格式化输出那块儿,用了string的format