- 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 递推 回溯 递归过程图示 用递归求解问题的特点 存在递归的终止条件 存在能使问题求解的递归方式 使用递归的优缺点 优点: 程序简洁, 代码紧凑。 缺点: 每调用函数一次,在内存堆栈区分
您可能关注的文档
最近下载
- Unit 2 Home Sweet Home Section A 课件 2025-2026学年度人教版英语八年级上册.pptx VIP
- 中国精神障碍分类与诊断标准第-3-版(CCMD-3).pdf VIP
- Unit 2 Home Sweet Home Section A Grammar Focus 课件- 2025-2026学年人教版英语八年级上册.pptx VIP
- Unit 2 Home Sweet Home Section A(Grammar Focus 3a-3c)课件 2025人教版英语八年级上册.pptx
- Siemens西门子工业Maxum II Gas Chromatograph Portal Reference Manual Maxum II Gas Chromatograph Portal Reference Manual使用手册.pdf
- 常规厚膜片式电阻器使用.pdf VIP
- Unit 2 Home Sweet Home Section A Grammar Focus课件— 2025-2026学年人教版(2024)八年级英语上册.pptx VIP
- 荔枝的贮藏保鲜运输方案.pptx
- 初中英语八年级下册 Unit 2 Home Sweet Home Section A Grammar Focus 融合式教案.docx
- 初中英语人教版八年级上册:Unit 2 Home Sweet Home Section A 2a-Grammar Focus.pptx
原创力文档

文档评论(0)