根据下面关系式,求圆周率的值,直到最后一项的值小于给定阈值。
2π=1+31+3×52!+3×5×73!+⋯+3×5×7×⋯×(2n+1)n!+⋯
输入格式:
输出格式:
输入样例:
0.01
输出样例:
3.132157
思路:得使用double类型
#include<stdio.h>
int main(){
int i=0;
double fz,fm,fs;
double yz,sum;
scanf("%lf", &yz);
fm = fz = 1;
sum = fs = fz/fm;
while(fs>=yz){
i++;
fz = fz * i;
fm = fm * ( 2*i+1 );
fs = fz/fm;
sum = sum + fs;
}
printf("%.6lf", 2*sum);
return 0;
}