海伦公式计算三角形面积

深信服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;
}

在这里插入图片描述
注意事项:

  1. 要求输入的三个数字之间为逗号,所以格式控制必须为%f,%f,%f,一开始将格式设置为%f %f %f,测试用例通过率为%0,花费了不少时间调试。
  2. 要求输出的Area保留两位小数,所以要控制输出格式,若不加控制,也无法通过测试用例。

血一般的教训,其实这道题很简单,但没有注意这两点,导致调试过程浪费了很多时间。以后看到题目要注意输入、输出格式!!!!!

发布了58 篇原创文章 · 获赞 3 · 访问量 2160

猜你喜欢

转载自blog.csdn.net/weixin_43936250/article/details/104424261