栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

CPT 102

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

CPT 102

1. Recursive functions                                           【递归函数】
  • 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                                               【阶乘函数】
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                                      【斐波那契数组】
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
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/820152.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号