> 其他 >
用公式 π/4 = 1-1/3+1/5-1/7……,编程序求π的近似值,直到最后一项的绝对值小于10-4为止.
人气:107 ℃ 时间:2019-12-09 02:20:23
解答
#include
#include
void main()
{
int s;
float n,t,pi;
s=1,pi=0,n=t=1.0;

do
{
pi+=t;
n+=2;
s=-s;
t=s/n;
}while(fabs(t)>1e-4);

pi*=4;
printf("pi=%.4f\n",pi);
}
推荐
猜你喜欢
© 2024 79432.Com All Rights Reserved.
电脑版|手机版