C语言程序设计课件(英文)C program language 之5 Function and Program Structure.ppt

C语言程序设计课件(英文)C program language 之5 Function and Program Structure.ppt

  1. 1、本文档共66页,可阅读全部内容。
  2. 2、有哪些信誉好的足球投注网站(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
C语言程序设计课件(英文)C program language 之5 Function and Program Structure

Contents Example Using Recursion: Fibonacci Series Fibonacci series: 0, 1, 1, 2, 3, 5, 8... Each number is the sum of the previous two Can be solved recursively: fib( n ) = fib( n - 1 ) + fib( n – 2 ) Code for the fibonacci function long fibonacci( long n ) { if (n == 0 || n == 1) /* base case */ return n; else return fibonacci( n - 1) + fibonacci( n – 2 ); } Recursion Set of recursive calls for fibonacci Recursion When input the number 12345 ,please output 54321 Steps: (1) How to output one digit : printf(“%d”,….); (2) How to output the code on the lowest-digits of N digits int unmeber. (3) The number is divided by 10, (4) The original problem is narrowed to the same question,but N – 1 digits (5) Test the codition , if true, execute step (6), otherwise get back to step(2)。 (6) End of recursion calling 。 The digits are generated in the wrong order: low-order digits are available before high-order digits, but they have to be printed the other way around. Recursion 1 2 3 4 5 5 1 2 3 4 5 4 1 2 3 5 4 3 1 2 5 4 3 2 1 5 4 3 2 1 Note: 1. How to predigest the problem . 2.How to control the end of recursion 。 Recursion Output the value in reverse order (int N) { if(N =10)then { output the lowest digit; N is divided 10 N---N-1; call function itself( N---N-1 ); }else output N; } main( ) { int x; printf(“\nEnter N=”); scanf(“%d”,x); int_turn(x); } int_turn(n) int n; { if(n=10) { printf(“%d ”,n%10); int_turn(n/10); } else printf(“%d”,n); } Recursion void move(char getone, char putone) { printf(%c---%c\n,getone,putone); } void hanoi(int n,char one,char two,char three) { if(n==1) move(one,three); else { hanoi(n-1,one,three,two); move(one,three); hanoi(n-1,two,one,three); } } main() { int m; printf(Input the number of disks:); scanf(%d,m); printf(The steps to moving %3d disks:\n,m); hanoi(m,A,B,C); } Hanoi

文档评论(0)

yurixiang1314 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档