1296:近似计算
Desccription
计算pi/4=1-1/3+1/5-1/7+....+1/n,
Input
n
Output
根据该算式计算的pi的值(精确6位有效数字)
Sample Input
100
10000
1
Sample Output
3.121595
3.141393
4.000000
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int n;
int i;
while(cin>>n)
{
int j=0;
double count=0;
for(i=1;i<=n;i+=2)
{
if(j%2==0)
count=count+1.0/i;
else
count=count-1.0/i;
j++;
}
cout<<setprecision(6)<<fixed<<count*4<<endl;
}
return 0;
}