通过三点用Java求三角形周长和面积 原
时间: 2023-12-08 20:05:06 浏览: 41
题链接:https://www.nowcoder.com/practice/4c7b8d4ad8c14f5a99c5d7e5fd5d5e7a
题目描述
输入三角形的三个顶点坐标,求三角形的周长和面积。
输入格式
输入三个整数分别表示三个顶点的横纵坐标,每个整数的范围是[-100,100]。
输出格式
输出两行,第一行为三角形的周长,第二行为三角形的面积,均保留两位小数。
样例输入
0 0 3 0 0 4
样例输出
12.00
6.00
算法
(数学问题,三角形面积公式) $O(1)$
根据三角形面积公式和勾股定理求解。
时间复杂度
只需要进行一次计算,时间复杂度为 $O(1)$。
空间复杂度
只需要常数个变量,空间复杂度为 $O(1)$。
Java 代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x1 = sc.nextInt();
int y1 = sc.nextInt();
int x2 = sc.nextInt();
int y2 = sc.nextInt();
int x3 = sc.nextInt();
int y3 = sc.nextInt();
double a = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
double b = Math.sqrt((x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3));
double c = Math.sqrt((x3 - x1) * (x3 - x1) + (y3 - y1) * (y3 - y1));
double p = (a + b + c) / 2;
double s = Math.sqrt(p * (p - a) * (p - b) * (p - c));
System.out.println(String.format("%.2f", a + b + c));
System.out.println(String.format("%.2f", s));
}
}
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)