设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 + L5 c" S. h' K' ]8 h! N  {

    ( |3 R  M0 H# o6 l0 ]/ I" X% ^8 f解释的不错
    2 g; D$ _1 |  L8 [# b8 {: B/ V& k! |- {% ~; T
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。/ Q$ g0 o5 S+ B% Y3 y0 i2 N. @- k, m

    ( }5 P: h% c" k% o 关键要素
    5 @6 D( g* Z2 W, Y) q1. **基线条件(Base Case)**  m7 j2 |6 ^+ t4 e7 P; a- w
       - 递归终止的条件,防止无限循环
    $ D+ i+ H) H, m5 B6 ^' {/ C; }0 s   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    % L0 g* T0 ~9 x# b- ?4 T) k( L3 s5 {6 v3 m" _( _
    2. **递归条件(Recursive Case)**
    6 }3 Q; J4 D7 P2 E% d1 b9 Z4 ~   - 将原问题分解为更小的子问题. F. j) h3 l; Y! x+ Z6 ]5 F! S+ ]7 n5 U
       - 例如:n! = n × (n-1)!
    3 U8 r! Y1 m: ~) R/ Y
    ; F8 z) |* o+ |6 R( n6 e 经典示例:计算阶乘* \( w( K; |/ C  |/ p
    python
    1 s4 H1 C& C; w, b' V1 k5 U% Kdef factorial(n):
    6 C4 |; I' c7 s( W+ m2 \: e, Y    if n == 0:        # 基线条件- _! S8 a' }4 |7 P9 R( v
            return 1$ s; ?1 m" l+ m& I! Y! U
        else:             # 递归条件1 N4 ]2 F" E8 T4 o2 Y
            return n * factorial(n-1)9 F$ X# ]! u. a- w' z5 l
    执行过程(以计算 3! 为例):
    ; D/ E9 ?9 O! k! A6 kfactorial(3)
    : X+ B7 S$ J) T2 p5 [# Y7 |9 K3 * factorial(2)
    2 m, S" s( W' J3 u* v/ n9 g) u3 * (2 * factorial(1))
    ' U' L: @& W' K: a3 * (2 * (1 * factorial(0)))
    % k9 h1 L0 J8 o1 I  r& N3 * (2 * (1 * 1)) = 60 N1 F, {) v7 O9 a

    ! X! D/ Q/ V2 o  j; V0 h1 j 递归思维要点, r; r2 ~' f" [, P# e
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑7 H; b1 B' r+ p. ^3 _
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)4 }. S" X/ s! i. H3 s7 r3 D
    3. **递推过程**:不断向下分解问题(递)% h# g: U* |$ `& u& ~9 K! _9 S
    4. **回溯过程**:组合子问题结果返回(归)
    $ c" g2 T0 O3 ~5 H" l5 ?; e
    . s% E" j. Q: H; L 注意事项
    1 I: @' }2 |3 }" r' R必须要有终止条件
    7 b( k& `+ s$ t$ P4 }4 j递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    2 K" K7 c+ o* W% B( x; ^7 o0 t& E某些问题用递归更直观(如树遍历),但效率可能不如迭代
    * Q% `9 g. c  @尾递归优化可以提升效率(但Python不支持); U: k/ K  G% M( e

    : k3 x* [# o) I& _' f% [3 W6 R 递归 vs 迭代
    : ?: b6 f  w1 l# r: M) @|          | 递归                          | 迭代               |
    3 g1 V! W& w/ I( A7 U- {5 n/ P|----------|-----------------------------|------------------|
    & T/ Y) Z- N0 Z9 F9 o- N/ r8 @* D3 q| 实现方式    | 函数自调用                        | 循环结构            |- B& g. T" H* u5 N  j3 W) M6 Z
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    ' W* L" B9 v; ?; s/ n- h| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |$ M) Z  c9 U! i0 s  ~6 V
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |8 B# U" F# f$ C8 a5 I3 w$ I. H1 w

    $ X" L/ B' w& g& R' z9 P 经典递归应用场景
    5 T: Z. i" m" Y( M1. 文件系统遍历(目录树结构)9 R  h( n8 O  X' \$ h' ]
    2. 快速排序/归并排序算法: c* t9 ?8 w2 Y, E% o2 z9 l0 L
    3. 汉诺塔问题4 v/ m9 v; N4 d- M
    4. 二叉树遍历(前序/中序/后序); F5 E, P$ f7 z+ C3 [1 F
    5. 生成所有可能的组合(回溯算法): v/ |- j7 D& e& e) ?4 u

    ) [( e( K3 s8 K+ r$ W2 Z试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情

    半小时前
  • 签到天数: 3088 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,  x. C( \5 W: U- _2 }8 w/ _
    我推理机的核心算法应该是二叉树遍历的变种。
    7 r1 t% a! G* W7 k2 v: ?5 H7 F8 d另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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 Z1 O# r& p- B) J+ EKey Idea of Recursion
    - q5 i: z* v' S9 ?8 a% m' ^
    : b4 d1 P3 b0 ^+ C; n. N6 U, Z4 ?A recursive function solves a problem by:
    - }) U) T) Z' L( Q1 Z* C- I
    ; i! w2 r* ?# n3 C    Breaking the problem into smaller instances of the same problem.
    ' W6 v; s& |* c7 Y) U
    * x0 P6 M7 l& D! H    Solving the smallest instance directly (base case).! M9 W8 ^. ?' f6 S. ^
      N* }) y. a9 c' A, D( M9 \
        Combining the results of smaller instances to solve the larger problem.
    3 H/ d' Z! _4 P( a( m* \
    - z& p; }% J& z) yComponents of a Recursive Function
    - }9 r7 f. B; h0 m- H6 o/ S, A! F& l' T
        Base Case:
    4 A7 x2 h: k( P  p. j1 L1 b: e) ?: k
    . i) T5 H" |2 D$ A" Q        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    $ K: @9 P. p  T
    5 M: K* k. c% @  y        It acts as the stopping condition to prevent infinite recursion.
    9 C# s  @' W- N3 h% o; R( y0 B) |% U
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    3 O' y! e6 i; R& l9 e9 y. a0 }4 _' h1 x: R( B1 m6 f6 q; M' E; d
        Recursive Case:, O- R1 k4 X- u" `" i& Y) m
    1 l6 L( f7 v6 d6 o; k' W
            This is where the function calls itself with a smaller or simpler version of the problem.: P$ j+ c# P/ F4 K5 |7 s
      _2 \7 Y7 Q- W) J- v
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    / x8 _3 C- x6 J% B9 @; }- w* A) c) q. M( x1 V1 V, W. }
    Example: Factorial Calculation) M% V- X# U' `7 R) {  f, B3 G: z5 m
    3 J& M# k7 h) e1 t& H/ 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:0 P6 r7 s% A' l

    ) e$ r  M% W4 i, q5 s    Base case: 0! = 1
    3 K5 s5 s, n) B4 x5 o6 T& O6 j: n4 m7 w
        Recursive case: n! = n * (n-1)!1 j6 p: [, ^" C' {: m
    & ^! t0 B3 Y/ S4 H% o7 o0 _
    Here’s how it looks in code (Python):2 F6 i5 ^+ t' e* [8 ?4 J; |1 t
    python
      b& p$ g3 [8 ~5 [- C
    ; l2 ], f. y7 m; H, r/ }7 f) X7 f- \/ d8 H( L& v
    def factorial(n):
    9 _8 I7 m8 h1 g( ]    # Base case
    : f, j( Y0 E" H# L' N, u4 `% D1 y* X: A    if n == 0:" O, d7 G+ [- o) J; n
            return 1
    5 Q4 n& X4 T7 R. s  I, d, J    # Recursive case7 d% T8 P. ?( f% L- p/ A- f
        else:1 f2 l- g' K  A" P9 i
            return n * factorial(n - 1)5 I' J7 V" b5 k
    4 M0 M! I  \  ]$ ]6 x
    # Example usage
    7 B- A/ f  |) g! e; j% Cprint(factorial(5))  # Output: 120
    ( H! e  ]& \5 ]$ r/ f; |
    + m4 `9 n( q3 s, t/ D" {1 DHow Recursion Works- \5 q) R3 B) Z/ t& k
    . j1 D; B) M, I, o- Q
        The function keeps calling itself with smaller inputs until it reaches the base case.
      R& K+ y# `7 Z9 l/ r# Y4 M  x9 D+ W5 p* |7 I+ H+ z( |5 f+ k
        Once the base case is reached, the function starts returning values back up the call stack.
    7 x$ S& q* h5 L* j; Z5 A% t
    6 u1 `6 I9 ^% C    These returned values are combined to produce the final result./ a6 f5 }/ Q9 b0 ~

    * i# d6 x$ e& o! q: FFor factorial(5):
    ) [5 Q" V; Z9 E  C3 b8 z
    ' j! k! H" [0 I% }2 A6 ^, _, r, l
    . g/ f9 H% R; N2 Xfactorial(5) = 5 * factorial(4)' Y( i) T! I% w- w
    factorial(4) = 4 * factorial(3)
    0 E) t) I( K- h" A3 y, m; M* }* Cfactorial(3) = 3 * factorial(2)
    " t4 G' d9 M4 G+ m- tfactorial(2) = 2 * factorial(1)( i! Y0 u5 U# w& t8 T+ ~2 B! k
    factorial(1) = 1 * factorial(0)' D$ Q- J; N3 G, y
    factorial(0) = 1  # Base case
    2 v3 u, S0 G2 T& l6 w1 h( L7 B: b9 K
    Then, the results are combined:; u4 F# B) f5 E/ [  a6 b# z1 @" K
    6 P/ ]  b2 R# u6 b  ^) B* L. [6 n
    1 o1 @. K8 Y: E$ ]& B2 y$ E* |
    factorial(1) = 1 * 1 = 1
    $ [. P, W8 I* C4 Yfactorial(2) = 2 * 1 = 27 F, h# m/ B7 U( [/ d/ f
    factorial(3) = 3 * 2 = 67 n8 d/ W" }' B0 V8 W9 ?
    factorial(4) = 4 * 6 = 24  Y9 j- H! {* X% a5 e4 U! E
    factorial(5) = 5 * 24 = 1204 M, u0 ?0 D) H5 j* M

    * `& B7 I/ F: |4 ?) nAdvantages of Recursion' Y3 J0 w- v2 A+ i8 L5 O3 }% w5 Y
    ) a6 `: H7 W. n$ N  i: h
        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).
    . M3 ~# G& E0 O+ Q& m4 U2 |5 @: F( N3 W. P) t- u
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    7 {" F- g. j' l1 U1 m$ D# F' Q) X& W. T! J: ~
    Disadvantages of Recursion
    3 u0 B3 B# a) f% V! ?3 e5 }' W, n% ~, j
        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.: h7 q4 [- @1 [+ N9 j# n
    & s# j  Q- v: K. C' y( b; f" I
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).. D( h0 u* E1 g, e  D
    . M% z) G& D0 z/ U
    When to Use Recursion
    3 o- \( U) F3 u4 n$ H  y
    " }) U9 o* a- z1 V: d    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).4 _. r8 B& |$ w: [9 U
    ; o4 ?7 E% ], v2 P# a! s
        Problems with a clear base case and recursive case.
    8 l6 ?5 t* _" [
    , w- Z* d% n  k4 ?5 l/ A! H3 j, ~3 \! C  ]0 ]Example: Fibonacci Sequence
    5 U4 j2 `3 t- R
    0 Q2 {/ _# C  c* l4 y5 O9 |& TThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:* `9 A! {5 `7 u8 X  k

    : }* ^% Q$ L/ V% ^; \. ~! g' g$ Q    Base case: fib(0) = 0, fib(1) = 1
    % k! _+ q6 ]! L8 |/ `( }0 y) U% {
    : r, m  |& f: d4 F! L    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    8 c/ |" w+ m+ a# V2 ]' [
    ) `4 N  g/ ^4 W3 j5 W/ kpython7 M# v) |$ o' Y1 ?6 v: q
    4 Z* ?  `$ w6 S, y  Q% \/ _

    : w5 F. U8 F# g6 T8 l9 Vdef fibonacci(n):
      o( l1 t+ u, @$ V    # Base cases: \; f+ C) N+ w1 I7 n
        if n == 0:- H, N- |( \+ N& ~* i# c
            return 0
    3 K: F, z2 Q( D, v9 x8 o7 Z9 V    elif n == 1:; J) w8 d9 m% W8 U( \' F& t, z1 \, e2 i
            return 1
    $ c" H1 k/ _  E0 B0 F8 {    # Recursive case
    # P' a2 z2 f  g    else:
    9 P+ }% y% s7 S' l( ^' m8 O        return fibonacci(n - 1) + fibonacci(n - 2)7 k3 ^8 J1 S" e, o. ]- ]
    % @% O% {: F9 F3 c! Y" u6 M
    # Example usage: D+ V1 \" t# n3 f, ]- ^. D6 B2 [
    print(fibonacci(6))  # Output: 87 K4 h5 b6 Z; D. Z5 E( [
    % C: j5 ^3 S( x8 S
    Tail Recursion
    # A2 J# p5 B$ ^/ v  G9 D" A, H( ?2 i) X2 t8 U
    Tail 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).
    , H6 ~- ]$ N$ I, O, ]% q. v" y) l  f9 m
    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-11-9 07:05 , Processed in 0.031148 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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