用下面公式求π的近似值。π/4≈1-1/3+1/5-1/7+…直到最后一项的绝对值小于10-7为止。
#include<iostream>
#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=" << pi << endl;
return 0;
}