java求三角形的周长
时间: 2023-12-09 22:00:58 浏览: 56
要求三角形的周长,我们需要知道三角形的三边长。
假设三角形的三边长分别为a、b、c。
首先,我们需要确定三角形是否存在。按照三角形的性质,任意两边之和大于第三边,则可以构成一个三角形。因此,我们可以判断条件:(a+b)>c 且 (a+c)>b 且 (b+c)>a,如果满足这个条件,则说明三边长可以构成一个三角形。
如果存在三角形,则三角形的周长等于三边长之和。即周长 = a + b + c。
最后,根据给定的具体三边长的数值,代入公式,计算出三角形的周长即可。
例如,假设三角形的三边长分别为a=5、b=7、c=9。
我们验证条件:(5+7)>9 且 (5+9)>7 且 (7+9)>5,满足条件,可以构成一个三角形。
那么,三角形的周长 = 5 + 7 + 9 = 21。
所以,这个三角形的周长为21。
总结起来,我们求三角形的周长,需要先判断三边长是否满足构成三角形的条件,满足条件后计算三边长之和即可得到三角形的周长。
相关问题
通过三点用Java求三角形周长和面积 原
题链接: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编译求三角形周长
你好,根据您的问题,我可以回答。用Java编译求三角形周长的代码如下:
import java.util.Scanner;
public class TrianglePerimeter {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入三角形的三条边长:");
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();
double perimeter = a + b + c;
System.out.println("三角形的周长为:" + perimeter);
}
}
希望能帮到您!
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)