zoukankan      html  css  js  c++  java
  • 三角形面积

    已知三角形三个顶点在直角坐标系下的坐标分别为:
    (2.3, 2.5)
    (6.4, 3.1)
    (5.1, 7.2)

    求该三角形的面积。

    注意,要提交的是一个小数形式表示的浮点数。
    要求精确到小数后3位,如不足3位,需要补零。

    答案:

    海伦公式或者叉乘积。

    代码:

    import java.util.Scanner;
    public class Main {
        private static Scanner sc = new Scanner(System.in);
        private static double a,b,c,d,e,f;
        public static void main(String[] args) {
            a = sc.nextDouble();
            b = sc.nextDouble();
            c = sc.nextDouble();
            d = sc.nextDouble();
            e = sc.nextDouble();
            f = sc.nextDouble();
            double x = Math.sqrt(Math.pow(c - a,2) + Math.pow(d - b,2));
            double y = Math.sqrt(Math.pow(e - c,2) + Math.pow(f - d,2));
            double z = Math.sqrt(Math.pow(e - a,2) + Math.pow(f - b,2));
            double p = (x + y + z) / 2;
            System.out.println(Math.sqrt(p * (p - x) * (p - y) * (p - z)));
            double p1 = a - c,q1 = b - d;
            double p2 = e - c,q2 = f - d;
            System.out.println(Math.abs(p1 * q2 - q1 * p2) / 2);
        }
    }
  • 相关阅读:
    Binary Tree Paths
    Implement Stack using Queues
    Path Sum II
    Path Sum
    Plus One
    Add Digits
    Missing Number
    H-Index II
    H-Index
    Ugly Number II
  • 原文地址:https://www.cnblogs.com/8023spz/p/10760715.html
Copyright © 2011-2022 走看看