本文以C語言為例,不用遞迴方式編碼如下:#include<stdio.h>longfun1(intn){if(n<=1)return1;returnfun1(n-1)*n;}longfun2(intn){inti;longm=1;for(i=1;i<=n;++i)m*=i;returnm;}intmain(){printf("%ld\n",fun1(9));printf("%ld\n",fun2(9));return0;}擴充套件資料用遞迴方式求解方法:#include<stdio.h>longfun(intm,intn){if(n==1)returnm;elsereturnm*fun(m,n-1);}main(){intm,n;longs;longfun(int,int);printf("輸入m和n的值:");scanf("%d,%d",&m,&n);s=fun(m,n);printf("s=%ld\n",s);}
本文以C語言為例,不用遞迴方式編碼如下:#include<stdio.h>longfun1(intn){if(n<=1)return1;returnfun1(n-1)*n;}longfun2(intn){inti;longm=1;for(i=1;i<=n;++i)m*=i;returnm;}intmain(){printf("%ld\n",fun1(9));printf("%ld\n",fun2(9));return0;}擴充套件資料用遞迴方式求解方法:#include<stdio.h>longfun(intm,intn){if(n==1)returnm;elsereturnm*fun(m,n-1);}main(){intm,n;longs;longfun(int,int);printf("輸入m和n的值:");scanf("%d,%d",&m,&n);s=fun(m,n);printf("s=%ld\n",s);}