设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    & a* D: R8 q- E
    * s% l: H: G% p: D0 L解释的不错
    3 e' _# W9 Q( R9 l6 r5 Q/ B# i
    : s6 O+ u1 t' c9 w8 H+ }$ T递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    4 e! o  f6 R0 O' K6 v/ x4 {  ]( O6 R6 r. ~& [7 q/ R1 X" G
    关键要素, a: }1 E8 l( v7 x2 S: K& _/ Y/ p
    1. **基线条件(Base Case)**
    5 U' e  r  w) F; u# d; ]! s% V   - 递归终止的条件,防止无限循环
    ) U) j0 Z/ h, c$ U8 y6 a   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    ) e% W; ^  l+ K9 q
    0 \5 w9 O" p$ }0 O2 y  i! T! t- w2. **递归条件(Recursive Case)**
    1 t6 [2 l4 i6 I8 T# ~, y   - 将原问题分解为更小的子问题
    & l* e7 X" u* [- J0 L5 u$ S$ U   - 例如:n! = n × (n-1)!
    % j  [1 R; u% S: V& H9 n& x- F" ?
    $ T5 a' M3 ~; {, V7 X% p2 M 经典示例:计算阶乘
    2 l  d' v4 U) Q8 tpython
    . ?7 E) T: j  _( [4 Qdef factorial(n):
    8 g8 R& e1 t6 v) L  K    if n == 0:        # 基线条件
    + m/ S) M1 ?# j4 N6 q. @        return 1
    - \6 \$ r6 _# c9 I) W    else:             # 递归条件5 M+ ~" r! w5 m3 Z( J; s; ~
            return n * factorial(n-1)
    - p0 p6 n5 p* d; q6 Z2 Q. b执行过程(以计算 3! 为例):9 ~" z- I/ R5 V$ Y
    factorial(3). g5 l4 v' _6 C/ j
    3 * factorial(2)
    ) R+ N/ H- e7 \4 F3 n% \3 * (2 * factorial(1))
    & a8 x7 n. J- |" i3 * (2 * (1 * factorial(0)))& G; B: _8 c. G% M8 z- Z
    3 * (2 * (1 * 1)) = 60 [" q4 b- G- z& N

    ; W6 v0 }3 E! t( ]2 ?5 E 递归思维要点
    " q0 V$ J5 \( ^( s1 ]& i1. **信任递归**:假设子问题已经解决,专注当前层逻辑2 h, z5 g* g# w# j
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    6 K; t4 J6 v1 o5 D+ z2 o/ ^- h7 g3. **递推过程**:不断向下分解问题(递)
    % x" E8 F- @; k/ z# I4. **回溯过程**:组合子问题结果返回(归)/ C# t& R9 h* x! K/ ^6 g
    , g- A/ a4 H9 ]; f. F" h
    注意事项
    . ?2 L. F7 N5 P1 J' v6 V5 R必须要有终止条件
    ; R) ~9 Z6 j2 q% ~递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    1 ~# t4 e  ]) ^7 o8 \8 I6 i6 h: b) S某些问题用递归更直观(如树遍历),但效率可能不如迭代' {6 t9 }. S6 @- D! u" W* h
    尾递归优化可以提升效率(但Python不支持)
    ! }1 v, C9 u7 d5 r8 N+ r
    0 ~3 B& z5 D. K 递归 vs 迭代
    1 [2 P. K) Q9 r. A/ T9 u, J% z|          | 递归                          | 迭代               |+ H3 F, @" b4 a  L
    |----------|-----------------------------|------------------|- O: ]8 O; T8 j0 ^, w
    | 实现方式    | 函数自调用                        | 循环结构            |
    7 k: v/ i) x# ?8 i' a! i: I4 F8 Y0 D: \| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |: q3 V* |! @1 i& v
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
      r0 _* m+ [8 t| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |! ^( [. b8 V3 Y

    7 l: S4 i8 ]8 N 经典递归应用场景. f5 z, {* l0 Y
    1. 文件系统遍历(目录树结构)
    ( N% J% q% y5 {" \) d2. 快速排序/归并排序算法  O+ j. k, U* J3 }& Y
    3. 汉诺塔问题
    4 c+ ^7 R" [5 O! u1 E) s4 w4. 二叉树遍历(前序/中序/后序)4 Q: G7 E# E' v/ ~6 _- n# l2 R
    5. 生成所有可能的组合(回溯算法)% J8 d, Y$ x& L6 M. W

    0 t! o1 p0 c! N! n, l* C试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    擦汗
    7 小时前
  • 签到天数: 3119 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
      N0 m: z, @& U( ?  C" s4 o% b9 P我推理机的核心算法应该是二叉树遍历的变种。7 i4 r% j2 r/ }8 M5 l- p& N
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:6 E: {% t* f! k5 P- y5 Y5 r
    Key Idea of Recursion
    / \( R: M5 J" q: @, V
    4 E3 b* H1 Y7 _7 C9 F5 kA recursive function solves a problem by:
    / }3 F: q4 X9 [  i& v+ ]9 T4 S, s& }3 P, G! L6 b
        Breaking the problem into smaller instances of the same problem.
    - t- ~5 q& j+ f9 D9 l, L( B- E  w/ M6 i% R- Z
        Solving the smallest instance directly (base case).
    : ?" ]4 t0 U# |- k: a& z0 V0 |3 E) G( w* @; a
        Combining the results of smaller instances to solve the larger problem./ F$ r7 ^6 q1 X/ i
    ' T4 K/ @" c) i$ k
    Components of a Recursive Function
    3 o& ^8 N, M9 D
    - y  D/ E% l: @+ m    Base Case:
    % z  {4 A! A. Y, l, M
    & h5 U) j# }; w# k) g        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.0 M4 v0 O" x7 w! I0 k$ V: [

      j3 R; T# _& o4 M& m6 R, `: |        It acts as the stopping condition to prevent infinite recursion.2 b( U  s- i* V5 m8 V2 W

    9 J$ f. i) Y; c1 ~        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.3 S- O5 s4 J& X
    9 l! u! K0 L. D
        Recursive Case:  m5 a! U+ j3 g' d  i& X  j

    4 X% E4 X  z4 ~+ K1 {0 ?. x5 l        This is where the function calls itself with a smaller or simpler version of the problem.
    * V3 R5 G! G+ }* Z( H9 b! S2 _
    3 G0 j0 r$ x% w        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    6 c% K+ I. a3 O, o0 \7 v5 `! h9 V; B! y: d% W3 {! Y: c
    Example: Factorial Calculation
    5 c4 F2 b  }8 V0 {5 Z! u* b8 |! y4 X4 D; o1 H, q/ T& ]
    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:
    7 o8 S# x: B: b: r. ^' ~
    8 ^# Y2 ]# A/ q    Base case: 0! = 1
    / h6 I7 g1 Z1 K. @' ]4 \, j" g) G  Z
        Recursive case: n! = n * (n-1)!
    + `, W6 q0 D% k' W! M; \# e$ Q7 f  }( X
    Here’s how it looks in code (Python):
    9 M% N% K4 ]6 c* fpython
    6 V/ v/ F3 f; q! s# o- @
    " O, q3 r' f1 J: z9 p! V4 E) Q# m9 }# g  r$ }, _( m) M* Z) m
    def factorial(n):
    7 n" C, g# k7 q/ z6 X% ?: H    # Base case) f  \& u5 J  J6 Z; a
        if n == 0:
    # ^0 |7 u6 y3 g8 H        return 1
    $ S( F' V: P0 P. g7 {# S    # Recursive case* F; F: B8 _- n/ [
        else:' {( _8 m. O, ~9 `4 @# M$ ]. @
            return n * factorial(n - 1)
    7 \% |6 X: f1 h' e- h1 }4 c/ q! B: L+ o; x. ]' R/ q5 \
    # Example usage
    ; \" N1 o- ~0 o; ~& _3 Y1 A- lprint(factorial(5))  # Output: 120. |% N# A8 N& a$ F+ i
    5 ?  m: w. m6 q7 Z
    How Recursion Works7 q% I: g: j# m+ y

    - K: e; L0 ?8 j, p    The function keeps calling itself with smaller inputs until it reaches the base case.6 o5 {" l3 m4 j) ^0 V
    / l4 q8 Y- E0 k3 Y+ G2 B% F
        Once the base case is reached, the function starts returning values back up the call stack.3 |9 x3 q" Q$ S. s

    / l6 r* D' B# j) ]5 A8 j    These returned values are combined to produce the final result.1 y5 W8 _! X2 [' W/ O  Z
    5 t8 o8 V1 E+ Y
    For factorial(5):& E% O' s2 `4 h, K% \/ h& R: w

    # n3 O, V: k7 J% N# \
    4 u* y9 N0 Y: x: h3 c+ X- zfactorial(5) = 5 * factorial(4)7 o; V- V/ j" i4 R
    factorial(4) = 4 * factorial(3)" }7 r! ], ]: @& R0 B9 g0 v! a
    factorial(3) = 3 * factorial(2)
    1 d$ ^6 Q7 ]* j2 f8 S  Ufactorial(2) = 2 * factorial(1)
    + P: F& {- E) e9 _+ Dfactorial(1) = 1 * factorial(0)
    , C: u! Q  f! w3 p9 [$ ~/ |factorial(0) = 1  # Base case7 \2 ~, Z; I2 V

    ' F3 |1 T0 J6 J( u9 m6 CThen, the results are combined:+ _# r( E$ {: V& U6 Y, L

    - V3 a2 H$ d% n0 f/ q  t/ C' R, r! ]6 e' P; m) h! b
    factorial(1) = 1 * 1 = 16 }& b* F7 m" Z& S% d  j% Q
    factorial(2) = 2 * 1 = 2
    0 }( U2 Z6 t% `5 N6 Ofactorial(3) = 3 * 2 = 6
    & |$ O5 [6 P; P9 L9 x; Kfactorial(4) = 4 * 6 = 24
    3 ~5 v+ j$ t; R' @7 Gfactorial(5) = 5 * 24 = 120" o! P( R  W! V2 w, e

    3 d) N: j$ v# H3 h0 c% d7 WAdvantages of Recursion9 n5 ]* V/ f6 i: T2 Z3 L

    3 b( v, i0 M7 H# t) W    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).( C* z  q% ^( F6 m

    ' x6 Z  z. J) G$ o' K; i    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    - K6 {" z  }; r6 x+ A. a  C9 O* L* u( o! ]. o  j& w' }
    Disadvantages of Recursion
    9 Q4 f# K  M# Y: Q( D* e% N6 E/ V  ^
      }! B/ e) R6 u" S# y    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.. x7 R- q: s' j+ h! ~

    - J9 y( z- W2 ^2 n: U3 M8 \    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    / F2 F8 J) l5 H4 }" T. y8 A8 E) ^1 K7 @  l5 o% b. z
    When to Use Recursion+ l8 I9 q5 E; Q- i7 d9 i! f, [$ t
    5 S& f- P. N$ N9 U0 e6 T" \' F
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    + w2 Z8 O6 u# C
    ) D4 L/ w' _/ j; S: Z% {8 B    Problems with a clear base case and recursive case.
    9 s3 w6 z$ b8 h- P1 z2 h
    3 G8 i0 a8 D" Y: X% p; a9 mExample: Fibonacci Sequence; f9 y" ?: }% q5 e+ Z6 e8 D' W: }4 K

    - M7 E2 F' a$ d* a. h; `The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:  o' N7 X& X( K0 H/ m
    1 g- M9 U2 s- H6 S% j
        Base case: fib(0) = 0, fib(1) = 1+ p8 r. f- o. x  I7 s
    5 d  S  U& Y, X
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    " A+ R1 L" \& C& s4 ?/ i- Z. E5 p, F1 i/ L1 d0 o
    python) s2 q! F' F) v: o  }
    ; h  P0 R' w/ ^6 i7 T! f' M

    ! j# K1 |( A; o  ^* e! Gdef fibonacci(n):
    & f2 s) U+ }! F; P& q. D    # Base cases9 n, U& H; X' H' d6 `% J' N% b* M9 Q
        if n == 0:# }9 w2 g. r, B9 y. ]
            return 06 U+ r4 v) |8 L2 J
        elif n == 1:
    & L4 |/ `5 N1 j  Z        return 1
    ( X2 t3 f' q( y    # Recursive case
    2 F# R( Q8 g0 G. h; J    else:) u5 N5 C% P* Z
            return fibonacci(n - 1) + fibonacci(n - 2)2 L$ ~& C  M8 A# N$ T
    4 ~9 p8 M' f' l- ?$ i% Q
    # Example usage+ I9 V/ L6 h' V' `& s7 I
    print(fibonacci(6))  # Output: 8( X2 o9 Z, D4 ~. K: [; n, u

    6 M' S( H3 b" y& a6 R, d) LTail Recursion
      Q" o4 v/ d. L+ A% z' J' h; T1 h/ j1 ^6 H
    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).+ |; X( g4 \6 U; w" t

    / w8 P/ b: t2 }# o6 e; SIn 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-16 15:33 , Processed in 0.029635 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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