e≈1+1/1!+1/2!+1/3!+...+1/10!求e的近似值,想知道我的代码哪里写错了
#include <iostream>using namespace std;
int main(void)
{
float e,y;
int n,i=1;
for (e=1,n=2;n<=10;n++){
i=n*i;
y=1.0/i;
e=e+y;
}
cout<<e<<endl;
return 0;
}
和正确答案差了1
n从二遍历,没有加上第一项,把最后结果加上1就好了
页:
[1]