设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 . i* n& |1 U# {

    6 O' f9 t& [4 I& p, Z解释的不错
    1 N3 v  ^8 Z& N4 P$ F0 d# a2 L1 q0 Y$ l$ L" C+ }
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    ) E8 x6 @2 W# R! T! M1 o, H$ ~' \7 }, K
    关键要素5 V' N1 A# v! x; a
    1. **基线条件(Base Case)**
      T; m& q  J6 Y9 p. {5 d& _   - 递归终止的条件,防止无限循环  V' f' T6 w5 W) K" ]/ w& L
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1" Y% K- @& C2 c6 v& z! W

    6 [' V1 ~" S5 D  I2. **递归条件(Recursive Case)**
    ' W/ J  L$ B5 e; ]/ X   - 将原问题分解为更小的子问题
    9 V+ Z$ r/ Y- c% A. H   - 例如:n! = n × (n-1)!
    ( n% H  h6 j' [9 }# G7 z+ ?) y6 n4 b" F% @" @5 A2 X
    经典示例:计算阶乘
    . u; C9 H1 ], G; K& A9 U; d! }python0 a. Q+ F3 R5 D7 U1 A8 [
    def factorial(n):% t  @: f( g* H4 H' U& s
        if n == 0:        # 基线条件: d2 r* w% R- F/ J
            return 1
    - L  z' w; I4 a/ z4 X# a$ [- i% M    else:             # 递归条件
    2 f& `. e5 ?2 D0 u" N8 X  m        return n * factorial(n-1)/ a$ L+ }- V* L
    执行过程(以计算 3! 为例):
    + @* h9 P9 i' X7 u' ]factorial(3)# G8 m5 j5 x" ~& W! \& Z2 A
    3 * factorial(2)
    / C5 p; I# P/ F; x  s3 * (2 * factorial(1))
    1 ~5 h' O: M1 n4 E3 * (2 * (1 * factorial(0))), A1 \3 [! m9 ^4 N9 ]) f1 Z5 f
    3 * (2 * (1 * 1)) = 69 n) d4 s2 B2 x! h
    & b" [- e( B5 t. M+ t6 a6 E& D
    递归思维要点9 p* J7 G5 Y5 o+ i6 g3 \; U% b
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑( f* @0 {; y5 M1 e" a( S
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    + H0 k; ]" J; \# A2 l3. **递推过程**:不断向下分解问题(递)% h6 {/ N# R1 ]* W/ [
    4. **回溯过程**:组合子问题结果返回(归)
    & t# g: `/ v5 k, T$ I! I# l7 l/ t4 g3 R$ n, V6 ]" J6 u) \( V% G
    注意事项
    # D  v: n6 a/ X3 K% R' r3 o6 P! I* X) d必须要有终止条件
    / |; f/ a$ @4 W- u1 }3 E递归深度过大可能导致栈溢出(Python默认递归深度约1000层)7 t7 S1 w# v3 x- j& N7 {5 D* L
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    4 D1 w9 P* M8 `. n1 V尾递归优化可以提升效率(但Python不支持)
    + O& j6 h: R5 U* L* k
    $ b$ M, D% Q2 k7 r2 g- ^ 递归 vs 迭代
    & M; }' T6 e/ F3 ^9 F' n|          | 递归                          | 迭代               |) k0 `$ C  m0 j; ]; o% `
    |----------|-----------------------------|------------------|
    5 H$ v! d1 y9 p: [/ g! |. Z| 实现方式    | 函数自调用                        | 循环结构            |/ Z6 e) X* ?8 {- }$ w7 C
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    4 N: T! I  H" b, j7 L" G% ~% c| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    , I3 i- O/ m2 K' Z| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    7 D6 r7 I; j( S2 B
    ( q8 \) D# k0 a0 L3 x) i8 k4 O. E 经典递归应用场景6 H( `# t, U5 {5 J' H8 W
    1. 文件系统遍历(目录树结构); m$ k6 g/ k/ ]7 n* I
    2. 快速排序/归并排序算法
    % K, p' X: k1 G2 ?: c3. 汉诺塔问题
    0 A6 a5 q- O) r3 O4. 二叉树遍历(前序/中序/后序)1 y2 N9 ^' X* p2 W) j3 U
    5. 生成所有可能的组合(回溯算法)
    4 M% @7 @; Y) E% r
    0 Z7 F6 o. @$ u. ]2 b1 s( F* e试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情

    3 小时前
  • 签到天数: 3217 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    : J+ {) k$ n7 N我推理机的核心算法应该是二叉树遍历的变种。1 f, K& l! ]' Q" C, K* g( [) 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:) n6 |3 }0 l1 V) s" ^9 d9 V
    Key Idea of Recursion
    9 U, n5 d7 }8 P% e- |3 B) s* v, ]8 O
    A recursive function solves a problem by:
    9 K7 |0 y: P4 u6 O; Z" k) y, t, b! v% S( }7 E# \
        Breaking the problem into smaller instances of the same problem.
    : n3 L" T2 Z  N  }5 ~9 o2 Q( B7 e3 g4 q4 Z
        Solving the smallest instance directly (base case).1 t7 @4 A  [, _/ Y) ?4 m) ~* o
    . U( Y8 e% A" F* v7 f
        Combining the results of smaller instances to solve the larger problem.
    . t  j* l) `0 O) m5 f, b* l6 z4 S. U7 @1 B) x) {) v
    Components of a Recursive Function& g0 x& p0 P# |' V( j7 }( {

      J! o5 {6 P# t! f. u" J) v+ Y' ]    Base Case:
    # p  \! w. x) |1 _% {; Y
    ( S0 b" I3 D* I6 H0 R5 g/ @( D7 C' U6 n+ O        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.; i% n) [4 W/ i4 ]- d2 A
    8 k& B5 z/ ~' V/ h/ b
            It acts as the stopping condition to prevent infinite recursion.
    ! Q1 ]! a7 }- p, `2 P* A: r; ?0 a/ j* z( Q  I  ~
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    * i0 e7 \* s1 j5 N. V; g3 f* V8 d$ _" v
        Recursive Case:
    2 W- [+ z. r$ w- k2 b- j
    3 H6 q& e4 E0 G- K( d3 @        This is where the function calls itself with a smaller or simpler version of the problem.
    $ I% H7 f; H. }
      s+ e* Q( e5 L! e$ l/ L        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).6 d/ |# L/ Y) N! }/ B

    . S; }+ w7 I5 Y" ]Example: Factorial Calculation
    1 |+ c! w4 t% C( P+ R) C
      Y0 |4 ~+ ^4 CThe 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:/ ^/ \2 e% z% Q& {0 v7 F; @# ^2 m- |
    . D, S! @  _+ q8 x& U5 X! U, O
        Base case: 0! = 1
    # e. y) h8 F! [( a+ M) C
    * j9 j& E- E- @8 ]  F# [2 j6 v) T    Recursive case: n! = n * (n-1)!
    5 ]4 D# L, X; s# [6 v, r# S" ?% W  z
    Here’s how it looks in code (Python):& g, e6 {0 @4 q! T6 c  V( R1 A
    python
    / s# W7 y3 s$ _% T
    4 X# u8 B" u  _' ]) v; W& {, \) i5 A7 ^5 p
    def factorial(n):
    2 z8 q# J+ [" k! {; l    # Base case( |# b. K, K. w1 Z2 o2 G4 C
        if n == 0:
    5 ?" f" H5 Y7 b0 p. K) r        return 1
    7 ~3 ~' j+ L/ A% Y1 T4 x    # Recursive case4 n! Z4 S( J# x( G6 j4 H) @
        else:
    - L4 _6 X' H$ c# V+ J6 G3 N+ \        return n * factorial(n - 1)
    # R# b1 E0 k. c' a; }- |+ V9 @* N2 Z9 r: i- b8 |1 D7 x9 l* M
    # Example usage
    ! N- S0 @- F) Lprint(factorial(5))  # Output: 1204 p9 |4 U( l3 O- ]4 r
    * J! K/ ?& H" @* k1 M! j
    How Recursion Works/ ^2 {3 b5 A% }, w+ Z. Z' i+ [
    - O! J1 b8 R8 O# c1 l+ E# Z: {
        The function keeps calling itself with smaller inputs until it reaches the base case.
    + Q% V# h9 h( Q% @. v8 O; h+ r2 ^! K9 ?2 c
        Once the base case is reached, the function starts returning values back up the call stack.  O2 c' n4 G3 [: q$ K& A  h3 ~4 B

    & ?! i; ?+ @- X( p; s2 t7 t+ V    These returned values are combined to produce the final result.  l1 ], j" q9 A  Q
    ( `1 b" p4 F3 s3 [& R, f! C0 D
    For factorial(5):8 S- s# h4 {6 ?: k7 K; f
    # V5 @3 t& E( Q% P

    3 {# H! Z. ]8 ~5 nfactorial(5) = 5 * factorial(4)
    & ?/ y' f, V  M5 j" a2 ?factorial(4) = 4 * factorial(3): w1 N+ O. ]3 Y8 W4 W
    factorial(3) = 3 * factorial(2)6 }) {2 I6 h4 g# w1 ~/ h
    factorial(2) = 2 * factorial(1)
    ) I4 G; J$ [+ Pfactorial(1) = 1 * factorial(0)& a. g" @3 Z, G: `- @' u
    factorial(0) = 1  # Base case
    8 r# K9 F3 u3 k1 t% t( c# v- f& v* N( U
    Then, the results are combined:
    9 V' L4 h) u# P
    , G& W3 g2 Q! M1 P, c7 w. m- j* X+ Z1 P; P. u( n* S
    factorial(1) = 1 * 1 = 1
    / T# T  @$ ?8 d& v4 y9 afactorial(2) = 2 * 1 = 2
    5 `$ m% @! x0 Y0 \! Afactorial(3) = 3 * 2 = 6
      G! E" A( D! c: {factorial(4) = 4 * 6 = 248 o5 a! }* ]4 [2 j1 Z7 S& |
    factorial(5) = 5 * 24 = 120! @: S$ G0 ^7 ^7 z5 O1 @- [( O" r
    3 E$ X2 [( ]6 A$ |
    Advantages of Recursion
    3 L" S! N0 ^+ |* M& F
    . U4 `" G! t' o5 z    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 c. H5 i1 ^$ |& C8 M& m

    8 |2 Q5 h4 C: A$ \. N* H! X6 L9 J5 e7 g    Readability: Recursive code can be more readable and concise compared to iterative solutions.2 P; `' Q; g: N: ?! A

    . b9 ~' Y% q/ J8 r" QDisadvantages of Recursion3 v9 |1 e3 y3 i5 j2 A; H
    3 G  @7 q; R, e/ t/ k
        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.
    1 W% I8 U' j) l& m" ?, A3 q" m: c+ B& K+ B1 u
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).7 ^) B5 W4 w& i  \+ \
    # @  Y( s) t8 S' i- C% y* D
    When to Use Recursion# I& ~$ w7 J. o8 T( J1 J: f% J" k2 O

    + K  a& }6 H, K" P; C* r+ _9 h    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    / ^4 ~9 f) T& N- w: W
    6 ~1 v" y7 [4 G4 N. X    Problems with a clear base case and recursive case.
    8 a, V) Z7 \; b+ j9 F5 j* y9 u5 l2 q" F0 Y- z2 i7 O
    Example: Fibonacci Sequence
    ; J0 I/ p, N: K0 e# h
    ! |7 F$ W9 n# b! P* hThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    / F( d# J/ P- \% {! E
    1 `, T8 Z! i( o7 D    Base case: fib(0) = 0, fib(1) = 11 O- C0 p4 v. C  C% w0 m
    , P- V& Y3 b' f& f! L, P8 r
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    1 M7 H2 z, d( f% r5 R
    6 f& G+ m. A1 i+ _. C- opython
    % q) i4 U1 j. L* g, c" ?3 _
    ) [( f0 h  q4 G* k* r
    ) z1 n7 ]" ?8 N3 M* e* n: xdef fibonacci(n):7 x* v+ W/ Z1 k) Q" e
        # Base cases
    , d. U$ c- r' f3 n  i+ V# ^% B1 e9 {    if n == 0:, \3 X; M" H9 d3 @% X2 Z
            return 0( _0 _' f; z# r8 H$ c% K' z/ p& r
        elif n == 1:! B; J) e, k+ K+ u6 [' o
            return 1
    # T6 ~! `0 {, {    # Recursive case/ s; ]% Q5 r/ K2 r" T& R& _7 h% s& V/ |
        else:- u' `5 a1 Y+ b: T% Z$ n$ X8 V
            return fibonacci(n - 1) + fibonacci(n - 2)& c" ?0 J. d7 n9 @% h* f
    2 |" p) x' @! Z. J; H# i
    # Example usage4 }, Q- \: F" M* D* L
    print(fibonacci(6))  # Output: 8
    9 f8 w3 p, o) B8 |& ~. J; R/ {- u2 @  M' B- I) }9 C; |3 F4 b7 V4 Z
    Tail Recursion
    7 _: M/ s- \( L. V2 m4 m0 T" o' Z2 U: W  q" ]' ~
    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).
    / [% }& s3 L' G4 ~' g$ X8 X: {# O) l" r7 z  {/ R. F7 t0 T
    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-4-9 10:57 , Processed in 0.055607 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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