|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
麻烦大佬们帮我把每一步注释给写一遍(傻瓜式注释)这个程序没理解
- #include <stdio.h>
- #include <math.h>
- void main()
- {
- int s; float n , t , pi;
- t=1; pi=0; n=1.0; s=1;
- while(fabs(t) > 1e-6)
- {
- pi=pi + t; n= n + 2; s= -s ; t= s / n;
- }
- pi= pi * 4;
- printf("pi=%10.6f\n",pi);
- }
复制代码
首先,要知道π的近似值求解方法:π/4=1-1/3+1/5-1/7+1/9……
- #include <stdio.h>
- #include <math.h>
- void main()
- {
- int s; float n , t , pi;
- t=1; pi=0; n=1.0; s=1;//pi是π/4,t是近似值中的一项,n是每一项的分母,s是每一项的±号
- while(fabs(t) > 1e-6)
- {
- //t,s和n初值为1,每次循环,n加2,s取反,t为s/n
- pi=pi + t; n= n + 2; s= -s ; t= s / n;
- }
- //循环结束后,pi的结果是π/4,然后再使pi乘4得到π的近似值
- pi= pi * 4;
- printf("pi=%10.6f\n",pi);
- }
复制代码
|
|