c语言方法实现1!+2!+3!+4!+.....+n!=?阶乘之和
方法一:递归方法:
方法二:非递归#include<stdio.h> int f(int n); int sum(int n); int main(int argc,char *argv[]) { int n = 4; int total =sum(n); printf("total = %d\n",total); return 0; } int f(int n) { if(1 == n) return 1; else return n*f(n-1); } int sum(int n){ int i; int total =0; for(i=1;i<=n;i++){ total =total+ f(i); } return total; }
#include<stdio.h> int f(int ); int sum(int ); int main(int argc,char *argv[]) { int n=4; int total = sum(n); printf("total = %d\n",total); return 0; } int f(int n) { int i; int sum = 1; if(1 == n) { return n; } else { for(i=n;i>0;i--){ sum = sum * i; } } return sum; } int sum(int n){ int i,tmp; int total = 0; for(i=1;i<=n;i++){ tmp = f(i); total = total+tmp; } return total; }