题目:V - u Calculate e
A simple mathematical formula for e is
链接:https://cn.vjudge.net/contest...
where n is allowed to go to infinity. This can actually yield very accurate approximations of e using relatively small values of n.
Output
Output the approximations of e generated by the above formula for the values of n from 0 to 9. The beginning of your output should appear similar to that shown below.
Sample Output
n e


0 1
1 2
2 2.5
3 2.666666667
4 2.708333333

思路:这个比较简单就是循环相乘就可以了;

小技巧:但这个有一个特别要注意的点,就是因为要输出有限位,即多余的0不可以输出,所以对于这种题目的时候,因为C中没有特定的直接控制只能输出有限位,所以需要用if分类讨论,即有限循环的把特定的位数给标记出来;

代码:

#include<stdio.h>

int main()
{
    double sum=1;
    printf("n e\n");
    printf("- -----------\n");
    printf("0 1\n");
    double tum=1;
    for(int i=1;i<=9;i++)
    {
        sum*=1.0/i;
        tum+=sum;
        if(i==1)
            printf("%d %.0f\n",i,tum);
        else if(i==2)
            printf("%d %.1f\n",i,tum);
        else
            printf("%d %.9f\n",i,tum);
    }
    return 0;
}

haixinjiazu
1 声望0 粉丝