如题所述
#include <stdio.h>
#include <math.h>
void main(){
int i;
double pi=0;
for(i=1;1.0/(i*i)>=1e-6;i++)
pi+=1.0/(i*i);
printf("PI=%f",sqrt(pi*6));
}
#include <math.h>
void main(){
int i;
double pi=0;
for(i=1;1.0/(i*i)>=1e-6;i++)
pi+=1.0/(i*i);
printf("PI=%f",sqrt(pi*6));
}
从计算结果看,这个公式只能精确到小数点后第二位,这个计算公式的精度不是太高。
温馨提示:答案为网友推荐,仅供参考
第1个回答 2019-01-09
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double sn=0,x=1,n=pow(10,-6);
while((1/(x*x))>n)
{
sn=sn+1/(x*x);
x=x+1;
}
cout<<sn<<endl;
return 0;
}
#include <cmath>
using namespace std;
int main()
{
double sn=0,x=1,n=pow(10,-6);
while((1/(x*x))>n)
{
sn=sn+1/(x*x);
x=x+1;
}
cout<<sn<<endl;
return 0;
}