《C程序设计语言》课程PPT教学课件(讲稿)第十三章 函数嵌套

本次课内容:递归调用 教学目的:掌握递归概念,利用函数嵌套进行的递归处理算法, 能建立递归结构。 重点:递归的概念和函数的自身调用过程(递归)。 难点:函数的自身调用过程 预习:函数的定义、调用、传值和嵌套。 maino void swap(int x, int y) int a=3.b=5: void swap(int x, int y) swap(a, b); =t; t=x: X=y;y= printf(a=%od, b-=%,, a, b) printi(“x=%dy=%dⅦ”,xy);
本次课内容:递归调用 教学目的:掌握递归概念,利用函数嵌套进行的递归处理算法, 能建立递归结构。 重点:递归的概念和函数的自身调用过程(递归)。 难点:函数的自身调用过程。 预习:函数的定义、调用、传值和嵌套。 main() { int a=3,b=5; void swap(int x,int y); swap(a,b); printf(“a=%d,b=%d\n’,a,b); } void swap(int x,int y) { int t; t=x;x=y;y=t; printf(“x=%d,y=%d\n”,x,y); }

、概念 函数不允许定义在另一个函数内,但可以在一个函数中调 用另一个函数,这种调用称函数的嵌套。 递归:某一事物直接地或间接地由自己组成。 递归调用:一个函数直接或间接地调用自身,便构成了函数 的递归调用。前者称直接递归调用,后者称间接递归调用。 递归函数与数学模型
一、概念 函数不允许定义在另一个函数内,但可以在一个函数中调 用另一个函数,这种调用称函数的嵌套。 递归:某一事物直接地或间接地由自己组成。 递归调用:一个函数直接或间接地调用自身,便构成了函数 的递归调用。前者称直接递归调用,后者称间接递归调用。 二、递归函数与数学模型

、计算n! 数学模型: n fact(n) n*ae(x-1)(m>1) 函数: maino long fact(int n) if(n<=1) Int x; return(1); long fact(int n) ese printf("input a integer") return(n *fact(n-D) scanf(“%dn”,&x) printf(“x!=%ldm”,fact(x);
1、计算n! 数学模型: 函数: long fact(int n) { if (n1 ) fact(n)= main() { int x; long fact(int n); printf(“input a integer”); scanf(“%d\n”,&x); printf(“x!=%ld\n”,fact(x)); }

递归调用过程: maino n 调act(5) Fact(5)=120 fact(n)= fact(5) nfact(n-1)(n>1) 5*fact(4 20 fact(4) 4*act(3) 24 fact(3) 3*fact(2人 fact(2) 2 fact(I)N fact(1 返回
递归调用过程: main() 调fact(5) Fact(5)=120 5*fact(4) 120 4*fact(3) 24 3*fact(2) 6 2*fact(1) 2 返回 1 fact(5) fact(4) fact(3) fact(2) fact(1) * ( −1) 1 n fact n (n1 ) fact(n)=

2、求两个数的最大公约数的数学模型 b (a%b==0) gcd(a, b ) gcd(b, a%0b)(a%b!=0) (a=bb=a%b) maino int gcd(int a, int b) if(a%b==0) int x, y; return(b) int gcd(int a, int b) else return(gcd(b, a%b); printf("input two integers); scanf(%d%dn”,&x,y) printf(“%dⅦn”,gcd(xy);
2、求两个数的最大公约数的数学模型 int gcd(int a,int b) { if (a%b==0) return(b); else return(gcd(b,a%b); } gcd(b,a%b) b (a%b==0) (a%b!=0 ) gcd(a,b)= main() { int x,y; int gcd(int a,int b); printf(“input two integers”); scanf(“%d%d\n”,&x,&y); printf(“%d\n”,gcd(x,y)); } (a=b;b=a%b)

小结: 1、递归 2、递归调用 3、递归模型 4、递归调用过程
小结: 1、递归 2、递归调用 3、递归模型 4、递归调用过程
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 《C程序设计语言》课程PPT教学课件(讲稿)第三十章 结构体数组.ppt
- 《C程序设计语言》课程PPT教学课件(讲稿)第十二章 函数.ppt
- 《C程序设计语言》课程PPT教学课件(讲稿)第一章 程序设计语言和库函数.ppt
- 《办公自动化课程教学大纲》讲义.doc
- 上海理工大学:《电子商务基础与应用》课程教学资源(PPT课件)第八章 网络促销.ppt
- 《Structure and Interpretation of Computer Programs》project5 The Meta-Circular Evaluator.pdf
- 《Structure and Interpretation of Computer Programs》web.pdf
- 《Structure and Interpretation of Computer Programs》st05project4.pdf
- 《Structure and Interpretation of Computer Programs》project2.pdf
- 《Structure and Interpretation of Computer Programs》project1.pdf
- 《Structure and Interpretation of Computer Programs》writeup.pdf
- 《Structure and Interpretation of Computer Programs》project0.pdf
- 《Structure and Interpretation of Computer Programs》collab work.pdf
- 《Structure and Interpretation of Computer Programs》q2 solns post.pdf
- 《Structure and Interpretation of Computer Programs》q1 sp05.pdf
- 《Structure and Interpretation of Computer Programs》q2 sp 05.pdf
- 《Structure and Interpretation of Computer Programs》q1 sp05 solns.pdf
- 《Structure and Interpretation of Computer Programs》lecture 26 webhan.pdf
- 《Structure and Interpretation of Computer Programs》lecture 23 webhan.pdf
- 《Structure and Interpretation of Computer Programs》lecture 22 webhan.pdf
- 《C程序设计语言》课程PPT教学课件(讲稿)第三十一章 指向结构体变量的指针.ppt
- 《C程序设计语言》课程PPT教学课件(讲稿)第十四章 变量的存储.ppt
- 《C程序设计语言》课程PPT教学课件(讲稿)第十五章 编译预处理.ppt
- 《C程序设计语言》课程PPT教学课件(讲稿)第三十二章 动态存储分配和链表.ppt
- 《C程序设计语言》课程PPT教学课件(讲稿)第三十四章 位运算概述.ppt
- 《C程序设计语言》课程PPT教学课件(讲稿)第十八章 一维数组.ppt
- 《C程序设计语言》课程PPT教学课件(讲稿)第十九章 二维数组和多维.ppt
- 《C程序设计语言》课程PPT教学课件(讲稿)第二章 数据类型和常量变量.ppt
- 《C程序设计语言》课程PPT教学课件(讲稿)第四章 数据类型转换.ppt
- 《C程序设计语言》课程PPT教学课件(讲稿)第三十六章 文件概述.ppt
- 《C程序设计语言》课程PPT教学课件(讲稿)第五章 数据的输入和输出.ppt
- 《C程序设计语言》课程PPT教学课件(讲稿)第二十章 字符串和字符串数组.ppt
- 《C程序设计语言》课程PPT教学课件(讲稿)第三十三章 共用体类型数据.ppt
- 《C程序设计语言》课程PPT教学课件(讲稿)第二十三章 地址与指针.ppt
- 《C程序设计语言》课程PPT教学课件(讲稿)第二十四章 数组与指针.ppt
- 《C程序设计语言》课程PPT教学课件(讲稿)第二十五章 指针与函数.ppt
- 《C程序设计语言》课程PPT教学课件(讲稿)第六十七 C程序的流程设计.ppt
- 《C程序设计语言》课程PPT教学课件(讲稿)第二十六章 习题.ppt
- 《C程序设计语言》课程PPT教学课件(讲稿)第二十九章 结构体类型与引用.ppt
- 《C程序设计语言》课程PPT教学课件(讲稿)第八章 选择型程序设计.ppt