求Sn=1!+2!+3!+4!+5!+…+n!之值,其中n是一个数字(n不超过20)。

相信编出来都不难,就是有几条要考虑,就是这个n的输入。

①n<0不能运算
② 0!容易忽略
③n=19和n=20就是double也表示不出来,这个要自己算,然后作为特殊情况考虑输出
④弱弱的问一句,没人用Int吧?

#include<stdio.h>
int main()
{
    int i,j,n;
    double t,sum;

    while(~scanf("%d",&n))
    {
        sum=0;
        if(n<0)            //输入非法
            continue;
        if(n==0)           //不能忽略
            printf("1\n");
        else if(n==19)     //特殊处理
            printf("128425485935180313\n");
        else if(n==20)   //特殊处理
            printf("2561327494111820313\n");
        else               //其他情况随便算
        {
            for(i=1;i<=n;++i)
            {
                 t=1;
                 for(j=1;j<=i;j++)
                       t*=j;
                 sum+=t;
            }
            printf("%0.lf\n",sum);
        }
    }


    return(1);
}

猜你喜欢

转载自blog.csdn.net/weixin_41133154/article/details/78778000