@今天的c语言又学废了呢----解三角形
2021-02-04
Triangle
For given two sides of a triangle a and b and the angle C between them, calculate the following properties:
S: Area of the triangle
L: The length of the circumference of the triangle
h: The height of the triangle with side a as a bottom edge
Input
The length of a, the length of b and the angle C are given in integers.
Output
Print S, L and h in a line respectively. The output should not contain an absolute error greater than 10-4.
Sample Input
4 3 90
Sample Output
6.00000000
12.00000000
3.00000000
这题看似简单
直接用高中学的公式
确实很简单
只是坑比较多(就是错了几次而已,真的,几次而已)
1.从精度上看
输入的时候指明了是要用int,但是输出的时候得用double
…
2.从计算公式来看
1)首先写上<math .h>的头文件
2)用三角函数的时候,需要将角度转化成弧度
… 这就需要在开头的时候定义一下pi
也就是 const double pi = 3.1415926
然后 sin ( x * pi / 180)
【谁能知道高考不过半年,小five竟然需要百度这些共是,实在是裂开,裂开啊】
友好如我,直接列出公式吧(光公式我就改了几遍。。。)
S = a * b * 0.5 * sin(C * pi / 180) ;
L = a + b + sqrt(a * a + b * b - a * b * 2.0 * cos (C * pi / 180));
h = 2.0 * S / a;
在表达式里,记得尽早用小数给运算转型,不然得到的值可能不准确。
还有那个sqrt,一开始我就把它漏了。
…
3.输出的时候
在这道题里,每个值是单独成行的(这个oj里一般是 a only space between each value,欻!这里竟是 \n !!! 小丑竟是我自己)
下面是完整的代码:
#include <stdio.h>
#include <math.h>
int main (){
const double pi = 3.1415926;
int a,b,C;
scanf("%d %d %d",&a,&b,&C);
double S,L,H;
S = a * b * 0.5 * sin(C * pi / 180) ;
L = a + b + sqrt(a*a + b*b - a * b * 2.0 * cos (C * pi / 180));
H = 2.0 * S / a;
printf("%lf\n%lf\n%lf\n",S,L,H);
return 0;
}