C++编程:根据公式e=1+1/1!+1/2!+1/3!…计算e的值,要求使用while循环,直到公式最后一项的值小于0.0001

#include

using namespace std; int main() { int i=1; float s=1,t=1.0,e=1.0; while (t>=1e-4) { s*=i; t=1/s; e+=t; i++; } cout<<"e="<<e<<endl; return 0; } 请问哪里有错呢?

调试了下,下面的代码可以在VC++6.0里正常运行。

#include "stdafx.h"

#include <iostream.h>

//using namespace std;          //这句我给注释掉了,否则老报错。
int main()
{
 int i=1;
 float s=1,t=1.0,e=1.0;
 while (t>=1e-4)
 { 
  s*=i;
  t=1/s;
  e+=t;
  i++;
  }
    cout<<"e="<<e<<endl;
 return 0;
}

这是运行结果。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-01-14
最后一项小于0.0001的t,你也把它加进去了。
第2个回答  2014-01-14

float s=1改为float s=1.0

第3个回答  2018-03-22
还能把名字空间的定义给注释掉?你学过C++?你注释掉后后面的语法为std::cout<<

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网