设为首页收藏本站

爱吱声

 找回密码
 注册
搜索
查看: 3140|回复: 3
打印 上一主题 下一主题

[科技前沿] 突然想到让deepseek来解释一下递归

[复制链接]
  • TA的每日心情
    开心
    2025-9-8 05:08
  • 签到天数: 3 天

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    * j3 N8 x$ M* u, y
    " W( p7 a& z; K6 V& A$ g解释的不错
    5 u9 A& {2 U6 j* X7 A' O/ w& _2 L2 z8 y6 n0 s. Y; s, w" w) H
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    / \2 h: y; C6 B1 f# M/ r, f- v% U0 [0 R  s7 |0 K
    关键要素& e7 Y9 J0 y1 p7 }" t
    1. **基线条件(Base Case)**( F+ K1 z. n% L9 I  g, J  U
       - 递归终止的条件,防止无限循环
    5 \, y0 w% D4 B- w  [- ]   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1# D; c, U' k5 Z% X" a& ?
    2 g2 x; X6 i4 a6 x1 g
    2. **递归条件(Recursive Case)**9 C; x3 U: R; B4 k6 B4 {
       - 将原问题分解为更小的子问题
    8 c- L* M7 G' F( X   - 例如:n! = n × (n-1)!
    7 Y9 I9 x7 u+ ^1 s$ {7 Z0 d
    6 Q# [9 E5 Z* K4 k; \3 n/ X! ` 经典示例:计算阶乘6 N( |: j- x7 ~; ^: G+ l8 q4 ~9 e
    python! h1 [* m0 J% ^" R" [
    def factorial(n):8 ^/ ?' Q9 Y2 v2 _3 o$ [) N
        if n == 0:        # 基线条件& h' \) i! b( g
            return 18 Z; d# U. y* B' g: R
        else:             # 递归条件/ r; l2 z. k  \9 c. b* b/ e
            return n * factorial(n-1)
    6 y' r0 ~- u; L' z$ z, K; T执行过程(以计算 3! 为例):2 p/ a7 K  K& \; d$ a
    factorial(3)/ G' j! F7 Q$ C8 v5 Q
    3 * factorial(2)
    : f& m; j& p# e& ~/ Q3 * (2 * factorial(1))" z" x) p* t7 U9 i2 e8 n  J
    3 * (2 * (1 * factorial(0)))& i% Q; a% v# ?. V) C* b* z
    3 * (2 * (1 * 1)) = 65 x- K0 t1 A& G) S5 W1 ^

    # `( a* `. ]3 k$ G& e5 ~ 递归思维要点
    7 J# P" k" d3 o1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    & }  b+ x$ S1 Q5 s' w2. **栈结构**:每次调用都会创建新的栈帧(内存空间)1 q0 W' L8 g9 @' }' b
    3. **递推过程**:不断向下分解问题(递)
    ; [' e, |2 J. Z- h" ^) f9 G$ u& T4. **回溯过程**:组合子问题结果返回(归)
    % S7 `' @. J7 w* R* z  f3 L5 I+ G1 m1 \8 ?
    注意事项8 d( [7 }7 t* k4 p9 O
    必须要有终止条件& u2 f4 o  w) n* s8 b; A4 _
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)8 R( a. f6 n5 K
    某些问题用递归更直观(如树遍历),但效率可能不如迭代8 B" r/ C# O2 X6 d7 E) E7 V9 `6 ?" Z2 p
    尾递归优化可以提升效率(但Python不支持)
    9 t* {& U- @" T* C
    1 {4 D. n+ t7 t- j4 m: C 递归 vs 迭代
    7 e+ y7 ]0 G6 h- H# H|          | 递归                          | 迭代               |, g+ a2 y: \0 m; u
    |----------|-----------------------------|------------------|  q. c5 Q' k! @
    | 实现方式    | 函数自调用                        | 循环结构            |. P4 F0 u) F/ [9 F; r/ _, X! e, M
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    5 A. D6 O! q  N| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    . Q* d& o9 f- g" |$ W| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    # r; k6 O; K, v4 J+ r6 R9 {- [8 v8 V( u/ N# H
    经典递归应用场景5 C9 p7 E8 i  J$ d2 F6 N! I
    1. 文件系统遍历(目录树结构)/ B* E. @5 t- v% e1 T
    2. 快速排序/归并排序算法
    . `! {. h7 {7 K6 |7 d3. 汉诺塔问题
      h, P$ P5 P5 Q) B6 F. f4. 二叉树遍历(前序/中序/后序)0 v* a6 A( u- T, E8 A" x
    5. 生成所有可能的组合(回溯算法)
    6 l) D/ k' a8 i. C
    & o# V. l, N+ Q3 \试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

    参与人数 3爱元 +26 收起 理由
    pcb + 4
    老票 + 16 涨姿势
    住在乡下 + 6 给力

    查看全部评分

  • TA的每日心情

    昨天 06:41
  • 签到天数: 3328 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,: B3 j; M* [. f" P
    我推理机的核心算法应该是二叉树遍历的变种。
    3 {/ {" n" K8 Z! A, o. Z另外知识系统的推理机搜索深度(递归深度)并不长,没有超过10层的,如果输入变量多的话,搜索宽度很大,但对那时的286-386DOS系统,计算压力也不算大。
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    板凳
    发表于 2025-2-2 00:45:59 | 只看该作者
    Recursion in programming is a technique where a function calls itself in order to solve a problem. It is a powerful concept that allows you to break down complex problems into smaller, more manageable subproblems. Here's a detailed explanation:
    3 x" K) v- U2 m* mKey Idea of Recursion
    ' B1 D* d; L6 K8 }" Z  h+ l3 N/ B& i6 \' s8 V
    A recursive function solves a problem by:; B# u5 e+ p* f  K. Y2 J

    * ?7 e! S% a- w5 O5 S4 o    Breaking the problem into smaller instances of the same problem.
    # H4 B1 M! E; R! f
    . D6 }0 l1 U# m+ ?3 b    Solving the smallest instance directly (base case).
    ' N( a9 N6 j1 `- ^8 w3 i9 g7 ^) T+ y7 S
    7 b; e2 k' @# ^6 J9 c+ h    Combining the results of smaller instances to solve the larger problem.3 w5 Y5 M8 H; W: w8 }7 L
    2 d9 z- V; t0 w: |5 a
    Components of a Recursive Function
    1 u+ `3 |9 k: G6 u: h
    / _& q- o! ?* d" V    Base Case:( ?4 Z# T+ e" f' v# B% Z
    $ `) h$ H( M6 v7 R5 v( ~$ U+ J9 v
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.: P0 C: L8 V2 \' E* k: r

    / u7 N- G) ]4 G4 y: w        It acts as the stopping condition to prevent infinite recursion.
    ( U, z- k3 u* c5 U' r2 D
    % X5 e6 c( B# i$ v        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    4 }0 B3 g- x7 a6 F
    ' n. v  C1 b& Q$ H, L2 P% p! T    Recursive Case:# E. F- I) l! y6 U( F

    % [3 n! T( s6 K( M% e        This is where the function calls itself with a smaller or simpler version of the problem.$ K( M5 d* }7 A" ]3 K

    + y! k* }. n$ O7 O" H        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    : g9 F. r; m$ @0 b
    1 S7 w- c: G1 m2 XExample: Factorial Calculation
    % v! p6 l# X& I9 K) d6 {4 a) W* i9 z8 j! d  _% z+ z( u
    The factorial of a number n (denoted as n!) is the product of all positive integers less than or equal to n. It can be defined recursively as:
    . V6 Z8 J( E' M" e3 x( `8 f/ O( w  X. w3 y+ r
        Base case: 0! = 1
    . k( e  Z- M. W6 ?1 Q+ F$ S5 ^+ ?* w$ w# t" A6 ?. R* d
        Recursive case: n! = n * (n-1)!
    , i+ ^0 X+ ], |7 x9 k+ t) O+ j: }# K
    Here’s how it looks in code (Python):$ ]) w3 N: L6 l+ W! _0 Z4 t
    python
    6 K5 N  A+ T  R5 W8 f! g4 w8 q
    , `8 C) o- ~% R9 x& I8 c; ?' J4 H- x  N4 t3 @5 {* H* h# X% c
    def factorial(n):
    4 U! Y/ b5 d) @8 S% |* V7 E2 l) e    # Base case7 c1 w* t) b5 A" a/ _
        if n == 0:
    6 Z5 c+ w7 G% i, E        return 15 ?3 _8 _# Z& i& l
        # Recursive case2 N3 V6 B8 W. i7 ]9 P$ {2 v
        else:" h$ }  F/ _' l! F  @9 n  G7 x
            return n * factorial(n - 1)
    # e9 d! T6 |& _; r* m
    - P& R# t- c: _# \% ]# ~+ @# Example usage
    5 N' M# n. d' h- ]' J- \print(factorial(5))  # Output: 120
    + x# C* R8 D! j, ~6 O
    8 l, A# P! R& e& P: z" Z3 qHow Recursion Works  D% H2 G3 G: }5 ^+ o6 `

    * ~" {* j  `, K5 U8 J0 A/ e    The function keeps calling itself with smaller inputs until it reaches the base case., \5 w. R6 L" N" Q4 f/ z  b# W

    # a) L9 ?( b; v) P: d    Once the base case is reached, the function starts returning values back up the call stack.
    1 w$ k/ M! N9 s1 t. a
    ( |$ \- J5 j- A; B4 D" s    These returned values are combined to produce the final result.
    - Z' ~5 r& [" p1 s9 v* X
    6 T: M4 p7 Z4 oFor factorial(5):: [9 G+ g8 e5 i' U3 p

    5 O5 ?1 `/ Y! }/ s# x9 U9 U- u( B
    6 a( ?  B, g2 e- Nfactorial(5) = 5 * factorial(4)/ d4 e5 `; N1 f7 g- T
    factorial(4) = 4 * factorial(3)
    # @. s0 A* M* Z7 ]factorial(3) = 3 * factorial(2); ]; I4 E0 z7 Y  O' [# p- w
    factorial(2) = 2 * factorial(1)+ y& y- K/ J* @& ^+ V: i
    factorial(1) = 1 * factorial(0)7 ^1 G/ F; E1 _5 g
    factorial(0) = 1  # Base case
    4 s2 T. [: P* C) [9 K& A2 r+ j' ~& F/ u+ l, q8 t: [
    Then, the results are combined:
    0 Z7 w: e4 [/ T/ ^6 j+ x! ~# I& ^

    0 k2 V6 G' e( H( xfactorial(1) = 1 * 1 = 1
    ; \/ H' i& W- F: v* j0 d! i3 \/ @factorial(2) = 2 * 1 = 20 i. n9 r6 ?4 b% c. a; i9 Y
    factorial(3) = 3 * 2 = 6
    % g/ l' y4 W8 w# k/ D: kfactorial(4) = 4 * 6 = 24: y5 B2 k7 Y, m
    factorial(5) = 5 * 24 = 120
    3 K; L0 L8 U# ]9 i
    7 ~. D3 T) `$ |; N% F. HAdvantages of Recursion
    / A" N$ B/ v5 ~. e( ^6 x6 K8 X2 y2 s5 `" e4 e
        Simplicity: Recursive solutions are often more intuitive and easier to write for problems that have a natural recursive structure (e.g., tree traversals, divide-and-conquer algorithms).
    9 K/ n. ^7 {* a4 o/ g# W
    # i1 |" D( P' I. F- ]+ J3 b- L! n    Readability: Recursive code can be more readable and concise compared to iterative solutions.# L) y! f8 g$ @' v

    3 ]& t# Y) S( B0 ^Disadvantages of Recursion5 V# ]  \7 N' J. x+ C
    ; a" S; I' U. j/ x
        Performance Overhead: Each recursive call adds a new layer to the call stack, which can lead to high memory usage and potential stack overflow for deep recursion.
    $ b  i& D. b9 I, M
    : w8 h# X) [# g: I5 {1 Z    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).& w/ Q" z; [8 A
    ' V# i5 n2 x$ V+ l9 x6 ^& C
    When to Use Recursion+ O' b  V" b: Y- j0 p2 P
      i8 V( _1 ^0 H0 a5 ~: o
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).( x% H* o  h3 a* F. E& Z9 O
    4 @5 f3 J1 p* f9 K# V, u2 R+ l
        Problems with a clear base case and recursive case.
    7 L' }, n' X" V2 k8 o+ L* ?1 M$ J- l! {/ K$ I& V0 W/ i
    Example: Fibonacci Sequence, S! t/ b8 U6 e* |9 t
    ) @* B* ~& I/ L2 T- Z. V3 T
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:7 p; i- P1 A- q( [/ H
    8 Q) @2 y7 g' f: B% P2 M
        Base case: fib(0) = 0, fib(1) = 1
    3 m0 [$ k7 p- b, E  B2 o7 x1 u1 x% _
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
      k. S% Z. S" |$ p/ k. L& \" p6 e. u- O" H* @2 @" a5 L+ `
    python
    / r1 V& g" `- X6 K4 C
    4 ^  ]+ ]( E* v' {# J3 O" c+ q* J, R: Y
    def fibonacci(n):6 ?( h9 n/ r, ~
        # Base cases
    - S, o/ @$ @, R3 q& z    if n == 0:6 X4 H( Z! X- v: ~( i- E
            return 0
    % q8 x4 |! _7 [7 I, b; C    elif n == 1:
    % p% P, Q! m$ v8 Q* g  Q  r        return 1
    3 X- Q, I9 a7 E2 j  A    # Recursive case+ @' f7 q! |2 b) X; r3 z
        else:
    6 Q# E* l- y; N' B        return fibonacci(n - 1) + fibonacci(n - 2)- @. H+ u8 `$ h. Z/ M

    7 l* v9 M, Q3 j1 b; V& y, `+ `% B# Example usage
    ! t# _8 U2 B+ P9 m. S9 j; |0 nprint(fibonacci(6))  # Output: 8
    8 \, @/ A3 ]! M3 D+ k! x  i% g- z/ W  L/ {) U% r/ `; _- v/ r
    Tail Recursion
    / n3 n: W- d+ _1 \( _  f5 y
    : \2 r# v- A$ P/ H) mTail recursion is a special case of recursion where the recursive call is the last operation in the function. Some programming languages optimize tail-recursive functions to avoid stack overflow, but not all languages (e.g., Python does not optimize tail recursion).
    4 U- U* j4 T3 N  x9 u$ M- Q" ^$ o! m8 w* B. L
    In summary, recursion is a fundamental concept in programming that allows you to solve problems by breaking them into smaller, self-similar subproblems. It’s important to define a base case to avoid infinite recursion and to understand the trade-offs between recursion and iteration.
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    地板
    发表于 2025-2-2 00:47:27 | 只看该作者
    我还让Deepseek 给我讲讲Linux Kernel Driver 现在的开发流程,让一个老同志复习复习,快忘光了。
    回复 支持 反对

    使用道具 举报

    手机版|小黑屋|Archiver|网站错误报告|爱吱声   

    GMT+8, 2026-8-20 05:56 , Processed in 0.062022 second(s), 21 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

    快速回复 返回顶部 返回列表