上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_19 Recursion 1

上海交通大学交大密西根 ■■ 联合学院·一 ◆] 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Vg 101:Introduction To Computer and Programming Recursion
Vg 101: Introduction To Vg 101: Introduction To Computer and Programming Computer and Programming Recursion

上海交通大学交大密西根 联合学院· UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University What? Most algorithmic strategies used to solve programming problems have counterparts out side the domain of computing.E.g,when you perform a task repeatedly, you are using iteration.When you make a decision,you exercise conditional control. Because these operations are familiar,most students learn to use the control statements for,while and if with relatively little trouble. However,you will have to learn to use a powerful problem-solving strategy that has few direct counterparts in the real world. That strategy is called recursion!
What? • Most algorithmic strategies used to solve programming problems have counterparts out side the domain of computing. E.g, when you perform a task repeatedly, you are using iteration. When you make a decision, you exercise conditional control. • Because these operations are familiar, most students learn to use the control statements for, while and if with relatively little trouble. • However, you will have to learn to use a powerful problem-solving strategy that has few direct counterparts in the real world. • That strategy is called recursion!

上海交通大学交大密西根 联合学院·一 ■ UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University What is and what is not? Recursion is a solution technique in which large problems are solved by reducing them to smaller problems of the same form. ● Notice:the italicized phrase is crucial to the definition.Otherwise it describes the basic strategy of stepwise refinement.Both strategies involve decomposition. What makes recursion special is that the sub- problems in a recursive form have the same form as the original while stepwise refinement is not
What is and what is not? What is and what is not? • Recursion is a solution technique in which large problems are solved by reducing them to smaller problems of the same form . • Notice: the italicized phrase is crucial to the definition. Otherwise it describes the basic strategy of stepwise refinement. Both strategies involve decomposition. • What makes recursion special is that the subproblems in a recursive form have the same form as the original while stepwise refinement is not

上海交通大学交大密西根 联合学院一 ◆ 81 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University A Simple Problem We will start our Recursion discussion of Problem:Print the characters of recursion with a a string,one per line simple problem: Input: “Vg101” Output: ·print the characters g 1 0 1
A Simple Problem A Simple Problem • We will start our discussion of recursion with a simple problem: • print the characters Recursion Problem: Print the characters of a string, one per line Input: “Vg 101” Output: V g 1 0 1

上海交通大学交大密西根 联合学院·一 ◆ 181 UM-SJTU Joint Institute ■ University of Michigan Shanghal Jiao Tong University Pseudocode Here is some pseudocode that would seem to do the job,even if it is a little vague. Recursion void printStr (string &str) { if the string is empty,return Print the first character Print the rest as a short string
Pseudocode Pseudocode • Here is some pseudocode that would seem to do the job, even if it is a little vague. Recursion void printStr (string &str) { if the string is empty, return Print the first character Print the rest as a short string }

上海交通大学交大密西根 联合学院·一 181t UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University The First Part of the Code The first part is easy to turn into real C++. void printStr (string str) ConsoleT console; if (Istr.empty ( { console.printLine (strLOJ,endl); i
The First Part of the Code The First Part of the Code • The first part is easy to turn into real C++

上海交通大学交大密西根 ·联合学院一 ■ 81 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Recursion code Now for the void printStr(string str) conceptual leap:we ConsoleT console; can use the same if (Istr.empty ( { function we are console.printLine(str[0],endl); defining to solve the printstr (str.substr(1,str.length()); rest of the problem! That is,we can print This is a simpler problem and the rest of the string it has the same form and by having PrintString it will converge at finite steps of call itself. iterations
Recursion Code Recursion Code • Now for the conceptual leap: we can use the same function we are defining to solve the rest of the problem! • That is, we can print the rest of the string by having PrintString call itself. • This is a simpler problem and • it has the same form and • it will converge at finite steps of iterations

上海交通大学交大密西根 联合学院·一 ◆ UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Conditions of Using Recursion Strategy The two essential conditions void printStr (string str) in which recursion can be ConsoleT console; used are You must be able to if (Istr.empty ( identify the simple cases for { which the answer is easily console.printLine(str[0],endl); determined. printstr(str.substr (1,str.length () } You must be able to identify a recursive decomposition that allows This is a simpler problem and you to break any complex instance of the problem it has the same form and into simpler problems of it will converge at finite steps of the same form. iterations
Conditions of Using Recursion Strategy Conditions of Using Recursion Strategy • The two essential conditions in which recursion can be used are : – You must be able to identify the simple cases for which the answer is easily determined. – You must be able to identify a recursive decomposition that allows you to break any complex instance of the problem into simpler problems of the same form. • This is a simpler problem and • it has the same form and • it will converge at finite steps of iterations

上海交通大学交大密西根 ·联合学院一 81 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Recursion Paradigm In general,the body of a recursive function has the following form: if(test for simple case) { Compute a simple solution without recursion. } else { Break the problem down into subproblems of the same form. Solve each of the subproblems by calling this function recursively. Reassemble the solutions to the subproblems into a solution for the whole
Recursion Paradigm Recursion Paradigm • In general, the body of a recursive function has the following form: if (test for simple case) { Compute a simple solution without recursion. } else { Break the problem down into subproblems of the same form. Solve each of the subproblems by calling this function recursively. Reassemble the solutions to the subproblems into a solution for the whole. }

上海交通大学交大密西根 ·联合学院一 181 UM-SJTU Joint Institute ■ University of Michigan Shanghal Jiao Tong University Code of Example 1 int main O string str "Vg 101"; printstr (str); return 0; void printStr (string str) ConsoleT console; if (Istr.empty ( console.printLine (str[O],endD; printstr (str.substr (1,str.length () ;
Code of Example 1 Code of Example 1
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_16 MATLAB environment short.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_15 Introduction to matlab.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》教学资源_Chapter 1 Introduction.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》教学资源_第二章习题与答案(第三版).doc
- 上海交通大学:《数据库系统原理 The principle of Database System》教学资源_第三章习题与答案(第三版).doc
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)chapter8 Views, Indexes.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)Chapter7 Constraints and Triggers.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)Chapter6 The database Language SQL –as a tutorial.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)Chapter5 Algebraic and Logic Query languages.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)chapter4 High-level Database Models.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)chapter3 Design Theory for Relational Databases.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)chapter11 The semi-structured data model Structured data.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)Chapter1 Introduction.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》教学资源_intro.pdf
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第二章 8086系统结构.pdf
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第一章 绪论(毛义梅).pdf
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第四章 汇编语言程序设计_习题及解答.pdf
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第四章 汇编语言程序设计.pdf
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第十章 串行通信和可编程接口芯片8251A_习题及解答.pdf
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第十一章 A/D和D/A转换_习题及解答.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_20 Looking Ahead.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Array and its Applications.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_examples on class design.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Expressions and Statements.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_files_DataBase Design.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Function.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Introduction to Computer and Programming.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Introduction to Vg101.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_objects and classes.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_programming style guide for C plusplus.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Random Number_Graphics.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Start with C plusplus.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_vector_string.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation 1.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_recitation 13.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation IX.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation V.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation VII.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation VIII.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation X.ppt