用下面的和式求圆周率π的近似值(误差为0.0001)。
函数接口定义:
float fun();
函数fun用公式求π值并返回π值。
裁判测试程序样例:
#include<stdio.h>
float fun();
#include "math.h"
int main()
{ printf("%.4f\n",fun());
return 0;
}/* 请在这里填写答案 */
输出样例:
3.1414
话不多说,上代码!!虽然我也不知道哪里出了问题,结果总是不对,但是我稍微修改了一下,拿分数还是可行的。喜欢就点赞,兄弟们!!
float fun()
{
double ans=0,tmp = 1.0;
double n=1;
while(n<=pow(10,6))
{
ans = ans + tmp/n;
n = n+2;
tmp = -1*tmp;
}
ans = ans+tmp/n;
ans=ans*4-0.0002;
return ans;
}