首页 > 试题广场 >

直角三角形

[编程题]直角三角形
  • 热度指数:5471 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
创建一个CTriangle 类,需要用到第二题中创建的类,即用3点来代表一个三角形,输入三个点的坐标,实现判断此三角形是不是直角三角形,并输出此三角形的周长。

输入描述:
输入第一行为样例数m,接下来m行每行6个整数分别表示三个点的横纵坐标。


输出描述:
对于每个样例输出两行,第一行根据是否直角三角形输出Yes或No,第二行输出三角形的周长,保留小数点后两位。
示例1

输入

1
0 0 3 0 0 4

输出

Yes
12.00
Java
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])));
        }
    }
}




编辑于 2020-03-20 19:29:19 回复(0)

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
发表于 2019-03-20 15:09:18 回复(0)

问题信息

上传者:小小
难度:
2条回答 5624浏览

热门推荐

通过挑战的用户

查看代码
直角三角形