酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

编写程序,输入三角形的三条边长,判断是否可以构成三角形,如果可以,输出三角形的周长和面积(结果保留1位小数),否则,输出 Can't form a triangle. 【输入形式】 第一行给出第一个输入 第二行给出第二个输入 第三行给出第三个输入 【输出形式】 第一行给出第一个输出 第二行给出第二个输出 或 第一行给出输出 【样例输入】 1 2 5 【样例输出】 Can't form a triangle. 【样例输入】 3 4 5 【样例输出】 The circumference of the triangle is 12.0. The area of the triangle is 6.0.

```python import math # 获取三角形的三条边长 a = float(input()) b = float(input()) c = float(input()) # 判断能否构成三角形 if a + b > c and a + c > b and b + c > a: # 计算周长 circumference = a + b + c # 计算半周长 s = circumference / 2 # 计算面积 area = math.sqrt(s * (s - a) * (s - b) * (s - c)) print("The circumference of the triangle is {:.1f}.".format(circumference)) print("The area of the triangle is {:.1f}.".format(area)) else: print("Can't form a triangle.") # 额外注意的逻辑点: # 1. 输入的边长要确保是有效的数值。 # 2. 计算面积时,要注意半周长的计算和开方运算的准确性。 # 3. 对于异常输入(如非数值)需要进行适当的错误处理。 ``` [2025-03-13 10:55:58 | AI写代码神器 | 243点数解答]

相关提问