设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 * \' o; t3 C. K! k; Y0 s- f3 b" a

    . L9 k# O* J) h. |% c解释的不错3 o7 F  c0 [4 L$ v! A1 g

    / k+ a' Y4 ]+ `& ~- S+ J, s) q: I递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    5 L: N7 L9 E/ B9 v* Z
    ' y8 ^$ r" A! f* ~4 ] 关键要素$ [" o* [0 Z& {! A+ u3 B
    1. **基线条件(Base Case)**3 b+ w! v# F3 k2 D$ k0 _
       - 递归终止的条件,防止无限循环3 t6 I( k. p+ S; A
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    9 x$ V* `2 m9 N( O5 O" Q! d; K$ ?8 R6 K" j* O: Z
    2. **递归条件(Recursive Case)**6 S1 t4 b8 `7 k5 J2 @* w$ D
       - 将原问题分解为更小的子问题5 P7 w# h5 j- `3 |" K6 r
       - 例如:n! = n × (n-1)!5 H% N) H1 a- N4 V6 M2 e6 b
    2 [& h1 h, K' U0 Z- u3 y
    经典示例:计算阶乘
    - u4 q; h& I) I+ F( Spython4 @- [7 r3 f2 D- ^( q. Z  z
    def factorial(n):
    7 d  ^+ {, p1 [3 M    if n == 0:        # 基线条件
    ) i! x! ^. A% j# Q9 l7 \        return 1
    ! U  H+ s" a" }' {! J4 J9 A    else:             # 递归条件
    # z; t2 V2 m& h4 [+ b5 `  [        return n * factorial(n-1)
    ' B" S" N( p8 C& \# {* l& k执行过程(以计算 3! 为例):! }& B; ^, R) p. n
    factorial(3)
    , X; Q: u- B( z3 * factorial(2)
    ) y0 T5 U- _7 y  ?. q& s3 * (2 * factorial(1))  Y9 d, m$ ^: ~( R
    3 * (2 * (1 * factorial(0)))5 \# ]4 ^& a( v
    3 * (2 * (1 * 1)) = 64 Y6 [4 [- S: g1 v$ W% @! d# R

    8 I) f  ~- O; w+ R' g" i 递归思维要点
    6 c, e5 {3 \# X5 S: Z4 \$ Y1. **信任递归**:假设子问题已经解决,专注当前层逻辑" H& S& y2 V% O$ G( `* X* B
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)0 i+ }/ B- i' h. Q% s5 z
    3. **递推过程**:不断向下分解问题(递)
    * _: @( E+ i. o( d4. **回溯过程**:组合子问题结果返回(归)" Z8 M4 e3 ]  C$ \% R; `
    ( ^6 J; s' P$ T+ Y# @
    注意事项
    5 Z% E" E! q  h! A6 P  d; P* C必须要有终止条件* c$ F& H# A; V% V
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层), l/ ^- A; f2 G/ q8 S+ }
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    , e" o/ [2 b/ g# n: _0 a尾递归优化可以提升效率(但Python不支持)& v& M  ^  Z( q7 U8 @
    : W8 m+ W5 R6 w' v
    递归 vs 迭代  @, B! M8 L  w
    |          | 递归                          | 迭代               |
    7 M. E  ~+ r+ U' g6 a! ?|----------|-----------------------------|------------------|
    # |8 j& \+ U7 T| 实现方式    | 函数自调用                        | 循环结构            |
    % p( ]- A( n# K| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |2 A  W( A' k2 m/ ^1 W2 U
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    $ D1 A- S' I. O| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |: S7 J. d* i* [& \0 z8 u

    0 [9 t5 x0 {7 E7 ?& X' ? 经典递归应用场景
    , P6 L. i8 l7 k1. 文件系统遍历(目录树结构)% l; x' E4 g4 b: {0 L$ T" n
    2. 快速排序/归并排序算法
    : `2 k7 c7 H, V0 i7 v  Z3. 汉诺塔问题
      c. C6 {6 @  {" \' i$ `; c* T6 p4. 二叉树遍历(前序/中序/后序)) g0 W- |( l4 }( k3 T( J( q) u
    5. 生成所有可能的组合(回溯算法)
    # Y0 E  Y# t7 J  [1 c5 O2 Q- X" \8 s# x2 b$ x8 c$ e
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    奋斗
    6 小时前
  • 签到天数: 3129 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    ! f5 U) ?$ _. f" D4 F我推理机的核心算法应该是二叉树遍历的变种。" l4 a* q( \2 w7 ^! B7 X
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:" R% B4 g* {/ v; f% \
    Key Idea of Recursion
    - f9 L- I0 J+ ^8 |) }
    & s5 @$ S: q1 E: A' tA recursive function solves a problem by:; h: {+ Y8 {" z

    % i. Z( ]8 P5 z; {, M  ?0 [    Breaking the problem into smaller instances of the same problem.
    " `& P% `' n2 `) b3 e: X/ m# w5 ]1 ?7 x7 G
        Solving the smallest instance directly (base case).* m$ f: a+ }, J

    2 ?( T8 {3 y) k& W! v* F: r6 |) P    Combining the results of smaller instances to solve the larger problem.' u, o0 v( M7 s3 S* L

    $ }2 R& f5 a( v% z9 b" lComponents of a Recursive Function, J/ |" A8 Y  a0 Q) J+ `
    * ?2 V% P# S) _$ f- Q4 a
        Base Case:, T! q' q3 N1 ^/ U
    7 u2 O) D4 k1 V( @1 b& s/ L" d
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    5 x* U( ~1 O" W$ ]; d+ F7 o4 i+ k9 b, f+ o5 w
            It acts as the stopping condition to prevent infinite recursion.- r6 C3 ]& a5 w
    ) k. i8 o7 Q, ?+ l
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1., C2 i+ s7 a$ H( q" O, r

    ; a# ]! q! \, p    Recursive Case:' f8 H( n, O& V; `
    4 }/ s7 ?% j: S% H- g. Y+ d
            This is where the function calls itself with a smaller or simpler version of the problem.+ Y: J3 Z8 m; v8 o

    3 F! ^, Y2 L3 Z6 h: _7 t        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).9 h$ w) ~& Y, o) R! J! U

    % _# m/ f7 X& m' L" c/ eExample: Factorial Calculation
    2 K/ t% G4 k  N: S3 [
    0 P* P: |1 o5 [$ X: i: PThe 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:
    7 n/ U- J2 o. {3 t. [
    / l$ Q1 X5 D8 |+ ?3 \. J* a" {    Base case: 0! = 1
    + z) S7 \; x, r% s2 Q6 T! l) K5 K
    ; R3 ?3 ?; }7 u3 V    Recursive case: n! = n * (n-1)!
    3 m. U) r% n1 t( t; ^; B, r' i8 e3 f4 T: J; J" G( N7 e
    Here’s how it looks in code (Python):
    # b( r, _  o% o+ g0 N: u: Bpython
    5 |) Q5 e7 M8 p1 c! l/ I& P5 ]8 g6 G  f5 s. `

    2 \5 C, W, c5 y# \& i7 S. Z" I0 Ddef factorial(n):' \, j" M' Z+ R. X/ Z! Z& C% K
        # Base case8 `: e" m2 d* z  Q6 W
        if n == 0:6 W8 A8 Z7 j& K% f9 h& J5 |
            return 1! M. @2 M- h+ [( H
        # Recursive case& X3 ]  H1 T$ ?; j) ^4 e5 @
        else:9 S: @" G+ f' J8 R) w; J9 N
            return n * factorial(n - 1)5 R" ^! M4 z2 p
    6 U# i! H" t6 `" {
    # Example usage  V& U+ O. j8 i! z5 o5 k& m
    print(factorial(5))  # Output: 120% Z" ]: X; Q" E: t% G% B  ]
    2 N/ |( v$ p% b
    How Recursion Works
    ( b+ q0 y# h1 p4 v* C
    , R5 ?& q& Y* ]7 y; l    The function keeps calling itself with smaller inputs until it reaches the base case.% p  _" d2 T9 j$ L; P! C% o
    9 }$ G  T9 X! A
        Once the base case is reached, the function starts returning values back up the call stack.
    ; y+ ]% X4 \; l' o- j6 g0 l2 P7 I- y; b4 s: R2 s  Y% D
        These returned values are combined to produce the final result.% _6 C+ J; k+ C/ ^6 ]

    * Q" P/ h1 |) c) O9 @" s7 {For factorial(5):3 X* k! k1 |# s4 X" h6 L

    7 C7 H# d; j) ~& h0 w4 \
    % J+ }& i6 ~0 t( bfactorial(5) = 5 * factorial(4)4 Z2 K8 M4 H& u1 [' o8 J6 `8 ]
    factorial(4) = 4 * factorial(3)
    ( N+ m8 X- m- D1 c8 Jfactorial(3) = 3 * factorial(2)
    5 X/ m8 j8 \' q, [4 u- A6 n! h/ ]factorial(2) = 2 * factorial(1)
    $ a9 B" i# P, P- m$ l* |! mfactorial(1) = 1 * factorial(0)
    7 X5 {7 H  U( }, m+ `  m% a: x- g1 qfactorial(0) = 1  # Base case
    * D" c; V" l+ f) V- h+ s! ~3 |# I# V' H  r* e! g1 \; X
    Then, the results are combined:
    ' `/ x1 w- i4 N8 Q- l
    # @2 q# ?  ?1 c6 G
    ! N& J3 o9 {( {6 q1 r. Z3 \factorial(1) = 1 * 1 = 1
    1 L: y3 c: ^- I6 Efactorial(2) = 2 * 1 = 2
    1 \5 ]  M# {; L- L; I- z6 Cfactorial(3) = 3 * 2 = 6
    # v" q" s# ^; Y- E' ufactorial(4) = 4 * 6 = 24, v' {  ^$ _/ e  B; d! t2 s
    factorial(5) = 5 * 24 = 120
    ; q$ G( j. S$ A) T* V0 M2 ^$ s( ]( Q2 c
    Advantages of Recursion
    + W% A/ p3 ]+ e3 J- Y
    + m! H6 \5 _) z$ l% K3 v    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).1 _: `8 G: ]; g& l
    ; V* q' y- q" M
        Readability: Recursive code can be more readable and concise compared to iterative solutions.! }; u; I6 F1 G% S7 t# G) d, l
    . O9 X9 x; y4 F2 |* o( t* {6 k
    Disadvantages of Recursion
    : L* r0 ~4 A+ G; U; V
    ) w) T+ P1 d8 l8 t    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.
    5 p% q; B" G% \  v5 D5 S: R' ]
    - B3 y7 V: L5 D0 l5 `    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    & X9 K: y7 m/ G$ Y$ V3 J/ G
    & Y" D8 f8 ~6 M/ |) A) J" EWhen to Use Recursion
    5 S, ~. h) E0 v, u
      R! r; z( w+ f4 z; I( C    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    9 M. x% b0 `* |
    * w% o8 m/ I2 ?# ~) D$ @# C4 j2 j    Problems with a clear base case and recursive case.
    " t$ \$ a1 p5 A1 N$ m! f6 S6 O: c5 m( U! T" o5 S
    Example: Fibonacci Sequence/ Z. n3 {- I$ e( x" l

    # q% _/ C1 K: x0 DThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:6 i" U# ^& b( m0 J

    + t1 J4 ^7 }" g$ l8 e    Base case: fib(0) = 0, fib(1) = 1
    ( E6 ]6 ^. E. f( Z& B1 w9 q
    ( C# {2 s/ r; k0 r& o* ]5 T    Recursive case: fib(n) = fib(n-1) + fib(n-2), T9 t) d' H( a: v  k

    - s  W; e0 F# i2 Y; B7 zpython2 W# T! a& S4 f- ^

    ( Z$ z+ N/ h0 N, E, w9 q  W. P6 \
    def fibonacci(n):
    3 n) S% T4 E; z    # Base cases: [) E+ y. T0 i' }" A+ t9 A
        if n == 0:
    - D( G7 C( b2 M, Q% D8 D4 S        return 0% @# }) B9 l% z9 ^- @
        elif n == 1:
    8 P8 V" }0 g2 j0 n  N. F        return 18 S. u) O8 [' X7 i
        # Recursive case, }: z+ D3 l) K- F( e5 b
        else:
    & k. q2 i" T  ^( c% B# R- }- c6 N        return fibonacci(n - 1) + fibonacci(n - 2)+ e7 V; p+ R- x* g4 I/ Q4 J
    8 P9 p! c6 F2 G2 f. n
    # Example usage/ W+ z- H$ }& [: y
    print(fibonacci(6))  # Output: 8
    # g/ o. l' N% H8 p+ V& I% U. z' U8 U9 s8 y2 _
    Tail Recursion
    ! \3 {) o( T1 Z" l; ^6 e& V
    2 t+ k) \8 j3 F' n6 P0 Y+ P% OTail 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).
    & r, N$ Z, f( ^+ M) N5 q# w2 f# _* ~, N% W2 C
    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, 2025-12-27 13:29 , Processed in 0.061421 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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