深信服2020春招题目一:
假设三角形的三条边分别为a,b,c,
海伦公式计算三角形的面积公式:
s = (a + b + c) / 2
area = sqrt(s * (s - a) * (s - b) * (s - c))
要求输入格式为:
6,7,8
输出为:
The area of the triangle is 20.33
我使用C语言实现此题目,以下是通过全部测试用例的代码:
#include<stdio.h>
#include<math.h>
int main() {
float a, b, c, s, Area;
scanf("%f,%f,%f", &a, &b, &c);
if ((a == 0) || (b == 0) || (c == 0))
{
Area = 0.00;
printf("The area of the triangle is 0.00");
return 0;
}
s = (a + b + c) / 2;
Area = sqrt(s * (s - a) * (s - b) * (s - c));
printf("The area of the triangle is %.2f",Area);
return 0;
}
注意事项:
- 要求输入的三个数字之间为逗号,所以格式控制必须为
%f,%f,%f
,一开始将格式设置为%f %f %f
,测试用例通过率为%0,花费了不少时间调试。 - 要求输出的Area保留两位小数,所以要控制输出格式,若不加控制,也无法通过测试用例。
血一般的教训,其实这道题很简单,但没有注意这两点,导致调试过程浪费了很多时间。以后看到题目要注意输入、输出格式!!!!!