根据公式e=1+1/1!+1/2!+1/3!+...求e的近似值,精度要求为0.00001。大神之需看一下我做的为什么不对。

/*#include<iostream> #include<cmath> using namespace std; int main(void) { int n=1; float s=0.0; int i=1; while(fabs(1.0/float(n))>0.00001) { s = s+1.0/float(n); n = n*i; i = i+1; } cout<<s<<endl; return 0; } 输出结果: 2.71828 我做的如下,不是一模一样吗?怎么就不对了*/ #include<iostream> #include<cmath> using namespace std; int main() { int n=1; float s=0.0; int i=1; while(fabs(1.0/float(n))>0.00001); { s=s+1.0/float(n); n=n*i; i=i+1; } cout<<s<<endl; return 0; }

#include<iostream> #include<cmath> using namespace std; int main() { int n=1; float s=0.0; int i=1; while(fabs(1.0/float(n))>0.00001) ; //这里有问题,将 ;去掉 { s=s+1.0/float(n); n=n*i; i=i+1; } cout<<s<<endl; return 0; }

希望采纳
温馨提示:答案为网友推荐,仅供参考
相似回答