【蓝桥杯】算法提高 7-2求arccos值

来源:互联网 发布:网络解决方案 编辑:程序博客网 时间:2024/06/05 10:15


题目链接:http://lx.lanqiao.cn/problem.page?gpid=T402


问题描述
  利用标准库中的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.1415926using namespace std;int main(){float x;double s;while(scanf("%f",&x)!=EOF){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;} 



0 0
原创粉丝点击