唐清穆 发表于 2018-11-17 00:12:29

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

dong628 发表于 2018-11-17 09:17:53

n从二遍历,没有加上第一项,把最后结果加上1就好了
页: [1]
查看完整版本: e≈1+1/1!+1/2!+1/3!+...+1/10!求e的近似值,想知道我的代码哪里写错了