第3章 模块化程序设计教案编写 .pptVIP

  • 1
  • 0
  • 约1.29万字
  • 约 64页
  • 2019-04-14 发布于天津
  • 举报
例: 输入一个非负整数 n,利用递归计算 n! f(n)= n*f(n-1) (n0) 1 (n=0) 分析:n!=n*(n-1)! 即 long int fact(int n) { long int f; if (n==0) f=1; else f=n*fact(n-1); return(f); } main( ) { int n; long int result; long int fact( ); while(1) { printf(Input a data: ); scanf(%d,n); if (n=0) break; } result=fact(n); printf(result=%ld,result); } 保证输入非负整数 n=5 result=fact(5) =5*fact(4) =4*fact(3) =3*fact(2) =2*fact(1) =1*fact(0) =1 result=120 递推 回溯 递归过程图示 用递归求解问题的特点 存在递归的终止条件 存在能使问题求解的递归方式 使用递归的优缺点 优点: 程序简洁, 代码紧凑。 缺点: 每调用函数一次,在内存堆栈区分

文档评论(0)

1亿VIP精品文档

相关文档