根据输入的三角形的三条边a、b、c,计算并输出面积和周长。注意:在一个三角形中, 任意两边之和大于第三边。三角形面积计算公式:area=s(s−a)(s−b)(s−c),其中s=(a+b+c)/2。 点击复制后,将打开C知道体验页
时间: 2024-04-30 07:22:42 浏览: 138
c++用函数写输入三边——判断三角形形状并计算面积周长
5星 · 资源好评率100%
```python
a = float(input())
b = float(input())
c = float(input())
if a+b > c and a+c > b and b+c > a:
s = (a+b+c)/2
area = (s*(s-a)*(s-b)*(s-c))**0.5
perimeter = a+b+c
print("area = {:.2f}; perimeter = {:.2f}".format(area, perimeter))
else:
print("These sides do not correspond to a valid triangle")
```
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double a = in.nextDouble();
double b = in.nextDouble();
double c = in.nextDouble();
if (a+b > c && a+c > b && b+c > a) {
double s = (a+b+c)/2;
double area = Math.sqrt(s*(s-a)*(s-b)*(s-c));
double perimeter = a+b+c;
System.out.printf("area = %.2f; perimeter = %.2f", area, perimeter);
} else {
System.out.println("These sides do not correspond to a valid triangle");
}
}
}
```
```c++
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double a, b, c;
cin >> a >> b >> c;
if (a+b > c && a+c > b && b+c > a) {
double s = (a+b+c)/2;
double area = sqrt(s*(s-a)*(s-b)*(s-c));
double perimeter = a+b+c;
printf("area = %.2f; perimeter = %.2f", area, perimeter);
} else {
cout << "These sides do not correspond to a valid triangle";
}
return 0;
}
```
```python
a, b, c = map(float, input().split())
if a+b > c and a+c > b and b+c > a:
s = (a+b+c)/2
area = (s*(s-a)*(s-b)*(s-c))**0.5
perimeter = a+b+c
print("area = {:.2f}; perimeter = {:.2f}".format(area, perimeter))
else:
print("These sides do not correspond to a valid triangle")
```
```c++
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double a, b, c;
cin >> a >> b >> c;
if (a+b > c && a+c > b && b+c > a) {
double s = (a+b+c)/2;
double area = sqrt(s*(s-a)*(s-b)*(s-c));
double perimeter = a+b+c;
printf("area = %.2f; perimeter = %.2f", area, perimeter);
} else {
cout << "These sides do not correspond to a valid triangle";
}
return 0;
}
```
阅读全文