STEP #1

(1/2)

此处切换ai服务商,可对比多家ai的回复,取最优解。

上一步下一步关闭
酷代码 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 # 输入三条边长 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 = (s * (s - a) * (s - b) * (s - c)) 0.5 # 输出结果,保留1位小数 print(f"The circumference of the triangle is {circumference:.1f}.") print(f"The area of the triangle is {area:.1f}.") else: print("Can't form a triangle.") ``` 需要注意的逻辑点: 1. 三角形的构成条件是任意两边之和大于第三边,因此需要检查所有三种组合。 2. 使用海伦公式计算面积时,确保半周长 `s` 的计算正确。 3. 输出结果时,使用格式化字符串 `:.1f` 来保留一位小数。 [2025-03-13 10:55:39 | AI写代码神器 | 236点数解答]
相关提问