#include<iostream> #include<cmath> using namespace std; int main() { int n; char ans; double pi; //for(int n = 0;n <= 20; n++) //{ do{ cout<<"输入n求pi的近似值:\n"; cin>>n; pi = 0.0; for(int i = 0;i <= n;i++) { pi += 1.0 * pow(-1,i)/(2*i+1); } cout<<"The result of pi is "<<pi*4<<endl; cout<<"Do you want again?"; cin>>ans; }while('y' == ans || 'Y' == ans); //} return 0; }
pi = 4[1-1/3+1/5-1/7+1/9…+((-1)^n)/(2n+1)]