题目
QQ图片20190331144301.png
代码
#include<stdio.h>
#include<stdlib.h>
//1+1/3+1/3*2/5+1/3*2/5*3/7...
// cnt/divident
//cnt++; divident += 2;
int main()
{
int divident = 1;
int cnt = 0;
double t1=1,t2=1;
double evenPi = 1;
double evenPiPre = 0;
double precision;
printf("输入精度:");
scanf_s("%lf", &precision);
while (((2*evenPi) - (2*evenPiPre)) > precision)
{
evenPiPre = evenPi;
t1 = t1*t2;
cnt++;
divident += 2;
t2 = (double)cnt / divident;
evenPi = evenPiPre + t1*t2;
printf("%f=%f+%f*%f\n", evenPi, evenPiPre, t1,t2);
}
printf("PI = %7f\n", evenPi*2);
system("pause");
return 0;
}
结果
输出.PNG
网友评论