当前位置:网站首页>CPT 102_ LEC 11
CPT 102_ LEC 11
2022-04-21 22:06:00 【NONE_ WHY】
1. Recursive functions 【 Recursive function 】
- A recursive function is a function that calls itself directly or indirectly
- Related to recursive problem solving: binary tree traversal (divide & conquer)
- The function knows how to solve a base case (stopping rule)
- A recursion step is needed to divide the problem into sub-problems (key step)
- Need to check for termination
2. Factorial function 【 Factorial function 】
2.1. Design
2.1.1. Base Case
- Number = 0 || Number = 1
- Result = 1
2.1.2. Recursive step
- Number > 1
- Result = Number x (Number - 1)!
2.2. Java Code
2.2.1.Java Code
-
import java.util.Scanner; public class Factorial { public static void main(String[] args) { Scanner kb =new Scanner(System.in); System.out.println("Enter the number"); int input = kb.nextInt(); System.out.println("Result"+factorial(input)); } public static int factorial(int input){ //input if (input<0){ return -1; } else if (input==0 ||input ==1){ return 1; }else { return input*factorial(input-1); } } }
3. Fibonacci function 【 Fibonacci array 】
3.1. Design
3.1.1. Base Case
- fibonacci(0) = 0
- fibonacci(1) = 1
3.1.2. Recursive step
- fibonacci(n) = fibonacci(n-1) + fibonacci(n-2)
3.1.3. How does it works?
3.2. Java Code
3.2.1.Java Code
-
import java.util.Scanner; public class Fibonacci { public static void main(String[] args) { Scanner kb =new Scanner(System.in); System.out.println("Enter the number"); int input = kb.nextInt(); System.out.println("Result: "+fibonacci(input)); } public static int fibonacci(int input){ if (input<0){ return -1; } else if (input == 0){ return 0; } else if (input ==1){ return 1; } else { return fibonacci(input-1)+fibonacci(input-2); } } }
4. Recursion vs iteration
4.1. Recursion vs iteration
4.1.1. Use
- Iteration uses an iterative structure, while \ for
- Recursion uses a selection structure & function calls
4.1.2. Termination test
- Iteration ends when the continuation condition fails
- Recursion ends when a base case recognized
4.1.3. Features
- Iteration
- Efficient but not easy to design
- Recursion
- Slow (cost memory & processor power) but elegant
4.1.4. Info
- Every recursive function can be rewritten as an iterative function
5. Q & A
- What is the key step in designing a recursive function?
- A recursion step is needed to divide the problem into sub-problems
- Every recursive function can be rewritten as an iterative function. (T or F)
- T
版权声明
本文为[NONE_ WHY]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204212205179263.html
边栏推荐
- 在线CSV转YAML工具
- Leetcode0785. Judgement bipartite graph (DFS)
- Makefile file configure executable file and cflags parameter
- Eventbridge integrated cloud service practice
- ROS机器人从起点到终点(四)蓝桥云实践复现
- Travel notes of provincial election and later basic planning
- Record a pit in the split3 Library (table name and field definition cannot use placeholders?)
- ServiceWorker 缓存与 HTTP 缓存
- Mswep data NC format to TIF format
- WPF数据驱动修改绑定的方法
猜你喜欢

字节日常实习(已OC)

Restcloud ETL out of the box - permanently free

ROS robot from starting point to end point (IV) reproduction of blue bridge cloud practice

Leetcode0785. Judgement bipartite graph (DFS)

How to realize the automatic message sending function of wechat with vbs

Echart writes a large screen showing a circular edge gradient histogram

In depth analysis of static, const, volatile, extern and register keywords

kotlin爬虫app,Android开发面试准备

Free your hands and recommend a low code tool open source by Alibaba, yyds~

WPF data-driven method for modifying binding
随机推荐
ROS——编译PCL相关程序报错:Could not find a package configuration file provided by “PCL“
Finding a way to combine new technologies with itself is the key to the development of industrial Internet
[best practice] patrol inspection item: local disk type inspection of cloud server (CVM) instance
【ES6】Generator
【WebGL】简单入门教程
如何将 ODBC 数据库与 PHP 连接?
echart 写一个大屏展示圆边渐变柱状图
[JVM] 10 interview questions
智慧化工园区解决方案
【ES6】函数的扩展
[Pinia] Chapter II core concepts
Free your hands and recommend a low code tool open source by Alibaba, yyds~
Huayun actively responded to the anti epidemic call of Hefei high tech Zone: practicing social responsibility and contributing to the strength of science and technology enterprises
Interview must brush algorithm top101 knapsack nine lectures Top13
Mypinpad and smartpesa merged to become the global leader in mobile payment acceptance
Record a pit in the split3 Library (table name and field definition cannot use placeholders?)
Outil CSV - YAML en ligne
LU decomposition, LDLT decomposition and Cholesky decomposition_ Zjuzly's blog - CSDN blog_ ldlt
Online yaml to properties tool
static,const,volatile,extern,register关键字深入解析
