方法1:迭代
cpp
#include<stdio.h>
int main()
{
long sum = 0,t = 1;
for(int i = 1;i <= 10;i ++)
{
t *= i;
sum += t;
}
printf("%ld\n",sum);
return 0;
}
方法2:递归
cpp
#include<stdio.h>
#define N 10
long factorial(int n)
{
if(n <= 1)
return 1;
return n*factorial(n-1);
}
int main()
{
long sum = 0;
for(int i = 0;i <= N;i ++)
sum += factorial(i);
printf("%ld\n",sum);
return 0;
}
运行代码截图:
注:侵权可删