C语言分别用递归法和迭代法求阶乘
阶乘的定义往往如下:
递归和迭代的实现请看下面的代码:
其实,很多情况下递归看似合理却埋下了陷阱,大大牺牲了效率,请看:C语言用递归求斐波那契数,让你发现递归的缺陷和效率瓶颈
递归和迭代的实现请看下面的代码:
#include <stdio.h> #include <time.h> // 递归计算阶乘 long factorial_recursion(int n){ if(n<=0){ return 1; }else{ return n * factorial_recursion(n-1); } } // 迭代计算阶乘 long factorial_iteration(int n){ int result = 1; while(n>1){ result *= n; n--; } return result; } int main(){ int N = 10; long recursion_result = factorial_recursion(N); long iteration_result = factorial_iteration(N); // %ld 输出长整型,即 long int printf("The factorial(recursion) of %ld is %ld!\n", N, recursion_result); printf("The factorial(iteration) of %ld is %ld!\n", N, iteration_result); return 0; }运行结果:
The factorial(recursion) of 10 is 3628800! The factorial(iteration) of 10 is 3628800!
其实,很多情况下递归看似合理却埋下了陷阱,大大牺牲了效率,请看:C语言用递归求斐波那契数,让你发现递归的缺陷和效率瓶颈