?? 例3.12.txt
字號:
例3.12 用下面公式求π的近似值。π/4≈1-1/3+1/5-1/7+…直到最后一項的絕對值小于10的-7次方為止。
根據(jù)給定的算法很容易編寫程序如下:
#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;
return 0;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -