用下面公式求π的近似值π/4=1-1/3+1/5-1/7+........

时间:2022-03-22 10:57:31
/*      
* Copyright (c) 2012, 烟台大学计算机学院
* All rights reserved.
* 作 者: 刘同宾
* 完成日期:2012 年 11 月 6 日
* 版 本 号:v1.0
*
* 输入描述:
* 问题描述:用下面公式求π的近似值π/4=1-1/3+1/5-1/7+........
* 程序输出:略
* 问题分析:略
* 算法设计:略
*/
#include<iostream>

#include<iomanip>

#include<cmath>

using namespace std;

int main()

{ int s=1;

double n=1,t=1,pi=0;

while((fabs(t))>1e-7)

{pi=pi+t;

n=n+2;

s=-s;

t=s/n;
}
pi=pi*4;

cout<<"pi="<<setiosflags(ios::fixed)<<setprecision(6)<<pi<<endl;

system("pause");

return 0;

}


用下面公式求π的近似值π/4=1-1/3+1/5-1/7+........