设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    8 Q. @  W  W9 c' S
    $ [6 f2 m$ n: u6 A9 ^解释的不错
    0 u+ o3 r3 V& R) J* D% J2 V5 E2 y! B1 j7 I+ }
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    5 p4 |& ]4 }; t
    ! A. s( h8 E3 y7 k! `0 ] 关键要素
    2 f. ~+ L( f' E3 ?1. **基线条件(Base Case)**
    2 d" W# ?' Q  K: G( R8 x   - 递归终止的条件,防止无限循环6 |4 i% p1 X, B1 p# P
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 11 V# a$ B* k  H" z6 p8 ?

    ' U; |) N" t( _2. **递归条件(Recursive Case)**0 S. A" h/ {- O4 w' L5 V3 m& e
       - 将原问题分解为更小的子问题' H( V4 R4 \- r& u
       - 例如:n! = n × (n-1)!
    0 u) j3 k0 i- I4 S0 m, n* f
    4 N3 m( a  ?% z) j. m+ Z0 x 经典示例:计算阶乘
    5 j5 _3 C# \/ L- Cpython
    ; C. {# u/ M4 O8 u: @def factorial(n):8 [7 b" |# `7 Y5 Q
        if n == 0:        # 基线条件" }/ Y, n7 O, E% U7 X: ^  t( |7 [& M
            return 1
    6 m2 Q6 I- c& D! k) E* @    else:             # 递归条件
    0 I3 @! z4 K. i: y$ [        return n * factorial(n-1)) S. A/ V' Q& Z. Y4 w: \
    执行过程(以计算 3! 为例):: J$ C4 p; F" W6 [/ k( m, ?
    factorial(3)3 d0 K. ]. j2 W* n9 R( P- z. n
    3 * factorial(2)# e( S4 E; o9 ^- M" c
    3 * (2 * factorial(1))
    3 Q0 w' N+ `6 ?8 L% Z% C+ q3 ^3 * (2 * (1 * factorial(0)))1 }4 S9 q$ ?6 X0 C
    3 * (2 * (1 * 1)) = 6
    : P" d5 d- ^( g+ s: e. y2 Y3 v. ^+ t- A
    递归思维要点5 V5 A6 T8 S3 K) T! n- I8 g
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    0 ~( a. i, t9 X" {2. **栈结构**:每次调用都会创建新的栈帧(内存空间)' a0 d$ F1 `/ a
    3. **递推过程**:不断向下分解问题(递)
    9 `0 a% j& H* `6 L5 i$ @4 G3 S. U4. **回溯过程**:组合子问题结果返回(归)" x1 ^7 {# ]) K+ \8 p
    ! n% T& [5 I. u  S8 D
    注意事项5 _9 n9 z& u" \0 u' b! K$ ]
    必须要有终止条件
    * P  q$ G4 P& F5 V递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    2 @- y' P6 K& f9 j5 _某些问题用递归更直观(如树遍历),但效率可能不如迭代
    . O  e  m! \# M. Z尾递归优化可以提升效率(但Python不支持)
    : x& G* A! ]3 ~
    / {/ e0 H0 N: I5 Y 递归 vs 迭代
    $ O  L6 F# u$ B5 J|          | 递归                          | 迭代               |3 L# n6 {* s5 j! @5 R3 x
    |----------|-----------------------------|------------------|' {# P3 p9 i2 M: f0 e
    | 实现方式    | 函数自调用                        | 循环结构            |
    . N9 K1 W1 a4 I| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |7 d/ ]1 b1 A4 c* ^! s
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    ( P3 E/ @! }$ n' u' _# G: S, l2 A| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    ) r- g% J" Y% P# Y4 c
    " b0 _  U7 z% M7 n  @- z% B 经典递归应用场景" }% K' k& h8 M2 }/ K* G; t
    1. 文件系统遍历(目录树结构)
    6 U0 o3 T& r5 k& l" M2. 快速排序/归并排序算法  c3 G5 K9 k2 |5 b+ q
    3. 汉诺塔问题
    8 v# D' i* k' x) z4. 二叉树遍历(前序/中序/后序)$ @& e( S( S0 x; C* i8 g* m
    5. 生成所有可能的组合(回溯算法)& H" V- ~. J/ m( |

    ) Q) J& X: o9 c7 l9 D9 P& D; {/ V1 u试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    郁闷
    昨天 07:20
  • 签到天数: 3145 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,8 u- O2 ]3 z+ e1 l
    我推理机的核心算法应该是二叉树遍历的变种。: x: W( ~. x  y6 P0 e5 K
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    7 J7 r  ]3 }0 b" v1 |Key Idea of Recursion
    # R& l- [; z2 u) ?" Q  q% a. m+ L# l" b) ~5 p/ I# |/ S
    A recursive function solves a problem by:, i7 r" ~: g6 l' j! ]
    ! K$ ?1 N5 q/ g3 F# N
        Breaking the problem into smaller instances of the same problem.
    2 T2 C0 |- r$ p' [: `1 l+ C9 m  v* W: f. `% [9 _' T
        Solving the smallest instance directly (base case).
    " ~+ @. |# v! r8 E" [2 o4 w. E8 T
      b! Y$ p  e7 O* h! R, Q; s. C    Combining the results of smaller instances to solve the larger problem./ l+ k2 ~4 r' R( g' i

    + B* p- v  x: Q* {) OComponents of a Recursive Function: l2 X, M! |8 l
    . {) z; \3 u& N1 @  r( P: b
        Base Case:/ u9 q" l$ ?3 U8 `# t
    - `0 g- m! I, x+ n2 z
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.' }% E+ X" W/ x8 o) S* Q1 T% f
    % _9 a% J) n3 \% I/ V  Z
            It acts as the stopping condition to prevent infinite recursion./ Z* S7 V8 Q  |

    " t6 A7 E( m7 `/ y' w        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.1 A  L; {$ |1 l$ W

    / ]( @( N' i9 O. ~" K! J    Recursive Case:
    3 @5 b! b$ t1 @" f4 R( }
    + R+ W% H& z$ f) A' K5 Y  }        This is where the function calls itself with a smaller or simpler version of the problem.
    $ J  J3 a, a" F2 F) q% g
    9 |' \# R" D# [$ S        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).: w% D* ^" _* `5 d
    7 c( i  q& w3 t6 q/ d! N% X
    Example: Factorial Calculation
    4 U% b$ ~$ V0 R0 @# `) o+ y: V% [9 a( v
    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:1 R. \* [* y6 `; E) m

    2 P1 @7 w# M; P( n4 s    Base case: 0! = 1# A& I% T' J- q3 z% R2 q$ G

    * s' M; L8 L% T. J: z5 G, |( x- m    Recursive case: n! = n * (n-1)!6 m! R# ?" M' i2 a2 M

    6 @4 H  w/ g  PHere’s how it looks in code (Python):
    6 L8 E: a( A, Q. O* m3 tpython
    : B4 K. m, V2 k; h  Y! n# S
    3 ]! C: C' r+ ]* h: V; w
    $ H' `; g/ }5 f' p. ~def factorial(n):
    1 G% _( j  O4 F; d7 T& x  l    # Base case" A) N9 I# i$ {" t4 Y- M
        if n == 0:
    3 V9 @3 q% c& ]# A' r  c" a; n; r% R; _        return 19 U$ e) x' M4 ^8 [2 J( j2 W! t# {3 N
        # Recursive case
    3 q4 X& m. N+ K3 R8 G    else:' b7 _( h( B1 C& b$ {
            return n * factorial(n - 1)1 k# O6 @( @8 C7 ]6 }1 r. N4 t/ X
    9 K; B" g* m8 U( h) p: K
    # Example usage( S+ {5 U* Y$ }' v
    print(factorial(5))  # Output: 120! F/ h/ t5 g( V' N% b

    4 y* \1 Q3 v) d- ~4 {How Recursion Works1 b. e5 n" J3 K0 x

    5 B( C  w4 H5 \6 E  c, y    The function keeps calling itself with smaller inputs until it reaches the base case., `. S; y  S! J. v3 ]6 g
    7 s9 |3 m3 d+ f! F
        Once the base case is reached, the function starts returning values back up the call stack.3 O! d4 {- B/ V0 l+ j8 E- q' k+ L
    % o  q5 {( ]( t. j( N# K" [8 k
        These returned values are combined to produce the final result.* l# t/ k4 J/ d/ p; w( w6 @1 }
    * |7 s2 q5 ~6 a, X$ n2 z
    For factorial(5):# ~5 N8 c5 g6 B. z# E5 t

    ! ~8 h) t2 x; V. [' e  c; o$ b% c* P
    factorial(5) = 5 * factorial(4)$ p5 g$ j: K1 e( _. V
    factorial(4) = 4 * factorial(3)9 H+ n" L  I3 p/ h+ T% x
    factorial(3) = 3 * factorial(2)
    1 o9 u4 M* \4 Sfactorial(2) = 2 * factorial(1)
    , g% J7 P# n  s/ y( [1 e1 H0 l  hfactorial(1) = 1 * factorial(0)& o$ q, D+ }' d4 g) l9 z
    factorial(0) = 1  # Base case9 E* u8 `$ g, X7 `$ L

    % e4 o$ F5 L+ cThen, the results are combined:
    8 K% t' i0 `! H6 T: T: r5 _2 s7 \* Y8 o0 ~# X) ~6 {

    : f2 W7 s: E) z0 n( X( C: hfactorial(1) = 1 * 1 = 1/ t9 y9 C- h+ S
    factorial(2) = 2 * 1 = 2
    5 U1 d' q. |* Q) k6 ~factorial(3) = 3 * 2 = 6
    ) F; L  t0 a: a2 F, Q- {factorial(4) = 4 * 6 = 240 L2 X( I) F. J( y
    factorial(5) = 5 * 24 = 120
    1 D; n3 E* T  r" U' C
    + |) H# t, S% JAdvantages of Recursion$ X5 d9 m8 F; J  d6 }4 ^0 w

    ; K' h  d( Q( b    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).6 @6 Q. m" X& ~7 ]
    ' _, x. ~1 C/ [
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    % a! M2 J. X2 O4 u' p3 C0 D8 k# s: l+ i. z
    Disadvantages of Recursion1 B2 A* {6 n3 v8 |2 |+ Q

    & g5 E' D# U0 A$ a# 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.
    # K- x' y, Z! X( q& i9 H& ?# G$ U: i; O' R8 F7 I
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization)./ }3 J' z+ E; k3 ]
    & m1 t2 N$ r  k8 w/ j1 U# {
    When to Use Recursion) a9 J" L( E+ V$ \6 _- d

    + F6 U: I+ Q* s7 R' k6 L  l    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    5 {7 U1 E- Q+ L$ T9 P+ x
    7 y. ]& V3 c) ]( y' \6 \7 }    Problems with a clear base case and recursive case.: R* P! h: H/ D$ v! [* b- t# L* R
    5 n3 [' d; W( }. Y0 H$ b
    Example: Fibonacci Sequence
    + }: n& W9 c/ O9 F! F/ {
    & o- G8 q$ C$ z7 ^! mThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:( w# W* G% {& ?/ p7 y' a

    ! j: R* g; B2 f" x- ~5 W3 Y    Base case: fib(0) = 0, fib(1) = 1
    ) _2 I2 z( o& i7 N
    6 g7 S+ M2 \' z' x    Recursive case: fib(n) = fib(n-1) + fib(n-2)
      }# t1 p6 V2 C! x* k8 _& H! o% @
    python
    $ M7 t* ~" a- o9 \. |2 z: L& v( A5 j% F$ r. y1 n0 E

    # N$ J/ p) R1 R" C$ tdef fibonacci(n):+ w4 u6 G. H3 l  @& H! V5 O$ w) g
        # Base cases; O! L# Y! R: Q8 T
        if n == 0:
    7 T( `1 U) I" z% j8 J; u! i% `4 S        return 0
    ! W4 l  ~4 {4 o1 L, c    elif n == 1:
    " g. }1 n/ a0 F1 u2 A  m' ~/ h% T        return 1
    1 ^3 m  J. U& V, m6 C: k* G    # Recursive case6 w' @, K7 Y' X. A" N9 y
        else:
    . Y8 ^2 u9 l2 q        return fibonacci(n - 1) + fibonacci(n - 2)
    ' c1 h. \7 q; S! Q0 t- N8 N3 t6 N2 A7 R* O- v) S
    # Example usage4 K/ S7 a# v( `$ B
    print(fibonacci(6))  # Output: 8
    # Z/ |$ U* s* Q0 S2 I
    , g$ q  a. X4 O* R/ ]  g' X( g  rTail Recursion
    . S: k  S/ u% K5 P
    3 o; S5 f+ `: p+ eTail 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).
    * K7 T. L9 `3 i. N# t) A
    ) [# P3 F0 B8 Z3 C" B9 d' w, \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-1-14 06:00 , Processed in 0.030539 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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