This question requires writing a program to a、b、c
calculate and output the area and perimeter based on the three sides of the input triangle . Note: In a triangle, the sum of any two sides is greater than the third side. The formula for calculating the area of a triangle:
area = s (s − a) (s − b) (s − c) area=\sqrt{s}\sqrt{(s−a)}\sqrt{(s−b)}\ sqrt{(s−c)} area=s(s−a)(s−b)(s−c)
Wheres=(a+b+c)/2
.
Input format:
The input is 3 positive integers, representing the 3 sides of the triangle a、b、c
.
Output format:
If the input side can form a triangle, in a row, according to
area = 面积; perimeter = 周长
The output format is to retain two decimal places. Otherwise, output
These sides do not correspond to a valid triangle
Input example 1:
5 5 3
Output sample 1:
area = 7.15; perimeter = 13.00
Input example 2:
1 4 1
Output sample 2:
These sides do not correspond to a valid triangle
Code:
# include <stdio.h>
# include <stdlib.h>
# include <math.h>
int main() {
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
double area,perimeter,s;
// 能构成三角形的条件
if (a + b > c && a + c > b && b + c > a) {
s = (a + b + c) / 2.0;
area = sqrt(s * (s - a) * (s - b) * (s - c));
perimeter = 2 * s;
printf("area = %.2lf; perimeter = %.2lf",area,perimeter);
}else {
printf("These sides do not correspond to a valid triangle");
}
return 0;
}
Submit screenshot:
Problem-solving ideas:
The difficulty of this question lies in the judgment of the triangle condition:
- The sum of any two sides is greater than the third side