算法提高 7-2求arccos值
时间限制:10.0s 内存限制:256.0MB
提交此题
问题描述
利用标准库中的cos(x)和fabs(x)函数实现arccos(x)函数,x取值范围是[-1, 1],返回值为[0, PI]。要求结果准确到小数点后5位。(PI = 3.1415926)
提示:要达到这种程度的精度需要使用double类型。
样例输入
0.5
样例输出
数据规模和约定
-1 <= x <= 1, 0 <= arccos(x) <= PI。
#include<cstdio>
#include<cmath>
#define PI 3.1415926
using namespace std;
int main()
{
float x;
double s;
scanf("%f",&x);
if(x==0) s=PI/2;
else if(x<0) s=PI-atan(sqrt(1-x*x)/(-1*x));
else s=atan(sqrt(1-x*x)/x);
printf("%0.5f\n", s);
return 0;
}