第1个回答 2015-06-30
#include <iostream>
#include <cmath>
using namespace std;
void main(){
double pi=0,t;
int i=1,f=1;
while((t=fabs(1.0/i))>=1.0e-6){
pi+=f*t;
i+=2;
f=-f;
}
cout <<"PI=" <<pi*4 <<endl;
}本回答被网友采纳
第2个回答 2018-11-07
这个可以用循环,循环继续调减就是大于十的六次方,先用通式算每一项,再相加,基本思路就是这样了
第3个回答 2015-06-30
int i = 1;
double sum = 0;
while(true){
double temp = 0;
if((i + 1)% 4 == 0){
temp = 1.0 / (i);
}
else{
temp = -1.0 * (i);
}
sum += temp;
i += 2;
if(temp < 1e-6)
break;
}
cout<<"结果为"<<sum<<endl;
cout<<"共计算"<<i / 2<<"项"<<endl;本回答被提问者采纳