7-41 计算阶乘和 (10 分)

版权声明:本文为博主原创文章,转载请附上博文链接! https://blog.csdn.net/weixin_43526304/article/details/85243882

对于给定的正整数N,需要你计算 S=1!+2!+3!+...+N!。

输入格式:

输入在一行中给出一个不超过10的正整数N。

输出格式:

在一行中输出S的值。

输入样例:

3

输出样例:

9

思路:第N项为前N-1项*N,利用这个规律 ,可以将每项的乘积都相加 

#include <stdio.h>
int main(){
    int N;
    scanf("%d",&N);
    int i = 1;
    int t = 1;
    int s = 0;
    while(i < N+1){
        s += t*i;
        t *= i;
        i ++;
    }
    printf("%d\n",s);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43526304/article/details/85243882