设为首页收藏本站

爱吱声

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

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

[复制链接]
  • TA的每日心情
    开心
    2015-11-30 11:11
  • 签到天数: 2 天

    [LV.1]炼气

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
      u' A1 ^9 j! K1 g
    ! ~, h. m3 L. g2 q解释的不错
      B6 G) e6 [$ m5 [, x/ v% n! R+ R6 S8 n9 |( }
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。! `" C# ?$ H" ~: G  g3 `. l- V" V
    - Z3 @. ~- C, K" p) s, j
    关键要素
    # U- U3 A8 z; _: L+ _$ y6 V# y1. **基线条件(Base Case)**
    0 [. }- M. U* n) u   - 递归终止的条件,防止无限循环+ R; G1 i6 z2 z$ p4 N; I7 N) z
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1; m' a, S3 l0 H+ W8 Q

    ' [, T6 H5 f! d2. **递归条件(Recursive Case)**4 a" [3 n2 l9 V. A9 W: `
       - 将原问题分解为更小的子问题
    , m1 }" {$ U8 D- C- {* E   - 例如:n! = n × (n-1)!
    8 ~7 D; o% N5 r& }; q* L# O/ W! J  V% q- R, W; A8 A
    经典示例:计算阶乘
    * W2 M; C4 Z9 e' [  ]% Rpython
    3 X3 O$ s. g3 W+ e, s7 J. cdef factorial(n):
    2 x# B8 [4 g8 s+ y2 h) E    if n == 0:        # 基线条件7 Q/ w" A  |: T! U1 d& d6 P* y, Z
            return 1& u$ d+ T6 T+ U3 v9 \9 e
        else:             # 递归条件
    ( r# D2 c# N7 w3 D9 U3 x9 x        return n * factorial(n-1)
    0 }0 P; A% q/ C- h执行过程(以计算 3! 为例):
    . r: V' R4 ~  L/ jfactorial(3)$ |6 b; Q: O" q4 k  R  J
    3 * factorial(2)
    3 \! \9 J+ u& J. s+ I' y3 * (2 * factorial(1))+ d# \. B( y- m, T3 V/ _* o5 D
    3 * (2 * (1 * factorial(0)))
    3 B) O# F7 t% @2 c+ f3 I! v3 * (2 * (1 * 1)) = 6
      y1 k  h8 A/ x8 n, @; X0 K$ |, R# b! A3 {6 A7 u6 `: D
    递归思维要点
    1 n" x/ t+ i8 }& Q4 Q1. **信任递归**:假设子问题已经解决,专注当前层逻辑4 M$ l1 l5 o9 c" L4 Z
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    1 G1 ?  m2 @- _) P% i! F6 c3. **递推过程**:不断向下分解问题(递)
    ' t+ v; @9 {9 E, e& T$ t+ X; Z4. **回溯过程**:组合子问题结果返回(归)9 h$ j" `) [  P  n5 ?7 `% B

      \' @) w' r7 @2 n8 M3 ~ 注意事项/ O. f5 @0 E) m' n
    必须要有终止条件
    " v. R# ], H( n1 F! _( L5 j递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    ) v7 y& e! T! D8 z  Z! \9 s- P某些问题用递归更直观(如树遍历),但效率可能不如迭代
    ) L; `& A1 v0 q8 g2 Y2 \6 R3 c尾递归优化可以提升效率(但Python不支持)
    ( b7 W# K; @0 a% ^
    % f2 {/ q+ U* H) l2 C 递归 vs 迭代
      |; B" y4 [# e9 y+ ~|          | 递归                          | 迭代               |6 S+ `2 C! A+ B
    |----------|-----------------------------|------------------|7 \3 Q7 W# z6 C% i) q; u# J- k
    | 实现方式    | 函数自调用                        | 循环结构            |
    1 Q6 Y" ]- {) ?3 k" e0 n| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |# u0 c1 M+ R6 }: b- O! A
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
      x' ?5 t# `8 F) x| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |8 _5 a/ w( m# ~$ S
    - i" U; c8 |% e3 v
    经典递归应用场景
    + v" a, W0 P! }: s5 x$ i1. 文件系统遍历(目录树结构)# T' v) b0 X. [$ R; ?! g
    2. 快速排序/归并排序算法/ z/ b* z7 p) M& m' \+ J8 w$ s# P
    3. 汉诺塔问题$ s* \+ x; N* ]8 t3 a1 R8 x: M5 ]
    4. 二叉树遍历(前序/中序/后序)+ ^( s: Y) c  o( K' N
    5. 生成所有可能的组合(回溯算法)
    . i& ?9 i/ P; T2 \. y5 x4 A+ `  h9 [( o
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    擦汗
    昨天 06:39
  • 签到天数: 3021 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,$ P  ?) ~0 c  }* Y
    我推理机的核心算法应该是二叉树遍历的变种。
    . h( D! a& v! T4 e. H另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:  v$ H  w! s6 b6 v
    Key Idea of Recursion
    ) W' z. M; w6 [! }5 A) F% J1 y  H' I1 M; i: d1 }9 v
    A recursive function solves a problem by:+ [1 M' F+ U( [# Q9 }

    ! f( ?( a" ~$ F* A; N. ^& G    Breaking the problem into smaller instances of the same problem.
    ; |$ h4 i2 T2 W" ^9 m: D: }
    + w( b% V' f( i8 `+ ^# i: ]7 M3 |+ \    Solving the smallest instance directly (base case).
    & y- b- V4 Q, |; d2 w+ e0 V
    ( @+ m% K# \" ?& I0 o' h    Combining the results of smaller instances to solve the larger problem.( u$ K& K* Z2 o+ H$ s  d" Z
    : j7 Y. m5 j2 C8 Q1 s1 r' v
    Components of a Recursive Function/ K' F6 C7 L+ f6 z& w% Y, ^) V
    2 ~" s$ ~/ H# L/ x9 P; Q4 r7 L
        Base Case:
    ! z- o& i+ w$ Q  p& N# U1 S
    5 ~- C3 Y* O; D  }" n- F" A        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.2 O! Z; N8 H$ N4 z$ g3 u
    , e7 N0 ^) v' I8 [3 |/ k& `) C
            It acts as the stopping condition to prevent infinite recursion., a; G5 j: f/ S9 n

    4 p9 C; o* q+ `        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.! ]7 I6 T# I9 g3 F
    ! X. b1 C0 M4 h. p1 o: K/ s
        Recursive Case:
    ( O; N: q* J- ]& H- U
    " s- O9 k" L; ~. A8 P* ]        This is where the function calls itself with a smaller or simpler version of the problem.+ S& u) l! x3 h4 s; J* ]7 G

    : N5 e* o# U0 t/ w; q        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).' h! }0 U$ c: C) ^9 X$ ~
      R/ c6 B+ a4 R; X9 i* h
    Example: Factorial Calculation
    ) C+ a# D& m! S  P" T2 @: b3 h/ O- R6 m- k' ?/ M
    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:
    & p2 Y; h% p3 D6 C
    / v& ~, Z8 Y3 s; [" Q! v7 Z% v    Base case: 0! = 12 F% Z' ~9 ~$ c  X; X9 L
    5 G/ q  n' k) y; g1 U
        Recursive case: n! = n * (n-1)!
    ' V& F7 E" J! ?3 }/ [& }& h, l) K8 J! E) c) ~+ O
    Here’s how it looks in code (Python):
    2 x2 e) `. ?& s) y( rpython! o& S- X# A0 \/ C, m: ]3 r1 e
    * m% f# J4 f9 G2 I

    3 D9 w5 K5 e* {# {% V5 qdef factorial(n):# a8 N, @0 S- h  Y' u. l6 T
        # Base case/ Q, q) x* x% @" G
        if n == 0:
    9 K; S. G+ @7 S# E# }        return 1% a9 G  G0 j# b! a, D( g7 h+ z8 v9 I  b
        # Recursive case
    " t" p  N( D* t$ b3 Z, K    else:
    " |9 l7 {& R/ N8 x5 `        return n * factorial(n - 1)
    ' n  S) I( V, U' E0 \# E. S
    6 T5 {0 V; V  R4 i, B0 v6 m# Example usage
    * w" x4 J. e0 @6 U) W& @print(factorial(5))  # Output: 120
    - C9 \9 Y' c8 s4 z8 B: ^6 [" [& ?; e# x" G% G
    How Recursion Works
    ' I! a3 y  R. y8 H) L
    2 l% s" ^6 y0 Y! n2 |6 c    The function keeps calling itself with smaller inputs until it reaches the base case.) \2 z0 a/ Q. }. `! F$ `4 {0 z& S

    : F4 `3 P7 J" c. p    Once the base case is reached, the function starts returning values back up the call stack.
    6 \3 O( j  {( X, `1 }* V/ F' c* c8 U6 r6 d% u9 ~, e
        These returned values are combined to produce the final result.
    5 `* I; a, {4 |* e" p& X9 k0 @: n: ]4 ^! G: G
    For factorial(5):
    # g" b7 ^; B! l- r9 c1 _; T/ M1 ~5 z. C' |& c* j

    , K/ T! {4 G6 A2 Ffactorial(5) = 5 * factorial(4)
    " ^2 J& j6 }6 f- N" ifactorial(4) = 4 * factorial(3)# n' }& S2 u" k) Y; T* T
    factorial(3) = 3 * factorial(2)
    ; ?$ Y! I3 O% E, Z# R3 u  Kfactorial(2) = 2 * factorial(1)/ p8 L; H) A2 O1 L
    factorial(1) = 1 * factorial(0)
    $ s, y( I, m* gfactorial(0) = 1  # Base case
    ; Q* `+ w3 D0 |5 f8 Y3 s
    / b0 ?) ?! o' k  Y' w8 G& K" T! uThen, the results are combined:
    . z9 W4 Z- [; _( m5 w; ]) K6 L* Y' {& B# E5 p' t  L
    7 `5 E! P9 v# w) F& Y: Q# n' c
    factorial(1) = 1 * 1 = 1' y8 a1 X. c- ~. G/ S  p
    factorial(2) = 2 * 1 = 2: X: b7 b9 u+ V- l4 }
    factorial(3) = 3 * 2 = 63 s  k: w- J. R# S7 d4 W
    factorial(4) = 4 * 6 = 24
    8 y0 F) [8 d1 K0 @5 B$ _; u  Wfactorial(5) = 5 * 24 = 120! w& O( D7 t8 F, n

    ' D$ z2 y* P0 ~0 t' |8 @0 @Advantages of Recursion
    # l& y! W9 r# e" c  w: p) u; n; }& P, o; q; f5 m) }! Q
        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).$ F  U1 N+ X7 W7 \

    6 k3 `4 d) N: F' f& G    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    / m6 t1 e+ ]! `; w
    & W2 l7 n; j1 T, a. [6 hDisadvantages of Recursion6 h3 |# U: s& w. u( ?

    2 f" A$ ^0 D! H6 h0 ]3 M    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./ _% ^, m/ o. G4 d) L# f
    1 O: h8 x! w4 }" o( }' v' k, @
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).2 Y4 u- o7 [7 k" {

    , u& x) a! L: r1 x! x- p2 UWhen to Use Recursion
    " w* h6 ?' O: k  U+ n4 t7 v7 L8 D" D
    0 Y$ G" t8 a1 i. p3 @    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).2 i  @  Y: I* \, K
    1 m8 K1 y0 A4 L( h. S
        Problems with a clear base case and recursive case.- w0 y7 t3 {7 ]9 [3 u' {$ P6 g5 [
    ; r/ L! U: O' P/ K
    Example: Fibonacci Sequence8 x  U7 U# j# O3 z; A: R
    $ ]* Q3 q) [" d2 K  u
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:: B: a- T- r( A7 W8 ^3 }0 J

    , @. y7 |' D8 E% x/ f    Base case: fib(0) = 0, fib(1) = 1. K7 F) G2 C" d5 _

    9 c1 B: f: X- Y! t3 ?  ]  J    Recursive case: fib(n) = fib(n-1) + fib(n-2)* b" x1 B9 N1 w; Y0 y% j: G

    1 N# ]: P9 ?9 \+ }3 q! Wpython
    9 X# T" {0 Z8 p% A( [" L
    + \# k& N0 T- O+ L. Q8 ~( h6 c; A' S4 a' \) T
    def fibonacci(n):
    / Y; T; h% O; E; U    # Base cases
    , V3 j) K4 }& k8 s6 A$ X6 Y7 n' R, a. p    if n == 0:3 F6 w% J# x+ T: |9 q8 l
            return 0
    ; @# Q; l" L4 ?5 }' J    elif n == 1:0 m, l9 [; V3 b% P+ Y! A! n. E+ P5 T
            return 1
    ; |' a! @1 q4 A( b    # Recursive case  r4 j5 b4 i; s: v
        else:
    2 e+ r* R: z0 Y; N        return fibonacci(n - 1) + fibonacci(n - 2)
    ) ?1 _2 p: O; r" a6 e8 m  e" n) _
    . @" e. f. N) ]$ z( ^3 k# Example usage" l: @+ W; y3 f
    print(fibonacci(6))  # Output: 86 V2 x4 u2 f5 B) |6 w

    * |& o4 e" j/ ]/ {/ VTail Recursion
    , z" K" X0 I- R& s* j" X3 X
    # Z7 _' O2 I& M9 o* uTail 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).
    " D) i- j5 s6 V1 W
    1 r6 ]4 q! a1 i3 b3 xIn 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, 2025-8-27 04:04 , Processed in 0.035077 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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