设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 + C4 ^' f. F& M; Q: W

    2 z( D6 d! N8 s2 _1 O解释的不错! D1 S( b7 ]4 ~) J, A0 B' G( t# D8 K
    2 _; P( d3 q9 z* d) o  |- T
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    " a2 R% e9 J$ S0 _7 P/ t( Q2 w
    " @; J- t4 K# Y% O- y& n6 T 关键要素( Y" {& z1 O4 ?6 A
    1. **基线条件(Base Case)**/ q) f( \* G  B6 ^" F, P, Y" n  Y
       - 递归终止的条件,防止无限循环1 J7 t; K* e, J1 J) ]
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1) ^! ]+ t+ Z  d
    ! \1 n  a5 M, ~$ H; ]
    2. **递归条件(Recursive Case)**
    2 c& Z6 v' j$ s9 V- x+ x( x) h   - 将原问题分解为更小的子问题
    3 i% `. K  j" x- z/ \3 L   - 例如:n! = n × (n-1)!$ g4 f+ ?. |8 d; l( f0 Z4 F
    4 I' S" f( g3 V( B- _
    经典示例:计算阶乘
    9 V0 U- t" K- @& ]& hpython
    - q6 \! U, Z; ]1 W( \" s8 fdef factorial(n):9 P+ K. g6 v5 e( K# d4 T7 n+ V0 m
        if n == 0:        # 基线条件5 f6 W# h3 F9 d* o) l/ c
            return 1
      \/ w; l& K; P' A. B* e. v    else:             # 递归条件) W3 Q8 z1 h& M2 L
            return n * factorial(n-1)
    6 N1 \3 a* r, g! n) ]& O. k执行过程(以计算 3! 为例):1 a: g/ Z1 {6 U( y# o& E* {/ A
    factorial(3)! E7 r/ \" C1 d7 |
    3 * factorial(2). l2 S+ s( A" L! _
    3 * (2 * factorial(1))3 v8 z( C& Y, G! m8 Z* g7 N0 ?5 f
    3 * (2 * (1 * factorial(0)))
    2 ]  Z$ h: R5 i+ d; {3 * (2 * (1 * 1)) = 64 s0 g7 j- Z- @! F3 ?

    ; s# Q5 g# F+ ~2 P: u6 ^ 递归思维要点# n4 [( C% S* x* E! v8 t; \
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    # P+ ]% \" ~' ~2. **栈结构**:每次调用都会创建新的栈帧(内存空间)0 z# y$ T8 t( u- b7 e/ `+ H
    3. **递推过程**:不断向下分解问题(递)
    5 _& B( x( A- n! D0 g4. **回溯过程**:组合子问题结果返回(归)+ u/ C' F1 z/ }" c

    : P1 X* J+ R" G6 x5 R, [# [ 注意事项1 V& C9 a6 Y% x! [' P) a+ @# e5 r6 k
    必须要有终止条件/ r5 i) D0 A. j6 a6 o/ E
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)9 H8 v9 y: G# _7 V2 Y7 L  s
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    & }; G/ @( G$ t8 T, a4 x8 x0 S尾递归优化可以提升效率(但Python不支持)
    . Q. X/ O9 M9 c  [3 [& S) x5 A+ J% J/ k' Y$ d5 [+ G+ P) i
    递归 vs 迭代1 H; B: N& u/ Y9 y" E
    |          | 递归                          | 迭代               |4 _9 x9 b% ~! D/ C+ b
    |----------|-----------------------------|------------------|
    / W( ~& v) U+ }6 Y3 V' x! T| 实现方式    | 函数自调用                        | 循环结构            |
    5 b( ]; e% P6 U| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    5 r4 N$ g5 a, g8 N" b" H) y| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    % Y4 ]# c! r' x: \% X| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    / V& h* v2 w  @0 g4 |2 P( k* q8 F9 o  G% x$ {0 U
    经典递归应用场景$ c8 W' k" I) n% |' w( }
    1. 文件系统遍历(目录树结构)' n# z, q$ i( F  z
    2. 快速排序/归并排序算法
    . b' `7 t7 y1 f! |: n, Q$ A! o2 D3. 汉诺塔问题
    6 ?0 Z/ u5 d6 ~9 f: S) w  \4 M0 D$ E4. 二叉树遍历(前序/中序/后序)
    * F2 t! F5 U2 d- G5 I' Y5. 生成所有可能的组合(回溯算法)
    : X0 f7 V/ o% e$ w+ o# Z/ B$ J5 y3 @! g' Z0 [9 g/ Y
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    奋斗
    昨天 07:11
  • 签到天数: 3150 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,# {9 H7 a" l' R$ E
    我推理机的核心算法应该是二叉树遍历的变种。
    6 r7 V0 E8 }4 }另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
      I: T& {  X6 }7 oKey Idea of Recursion
    + z: I+ o7 J' N, I' a, j3 p1 C
    # p5 `$ m& e- h. K3 X4 j9 L% q& uA recursive function solves a problem by:1 B) Y5 V8 o9 d* ?/ {' ]
    2 `9 i  s" K5 J0 V1 N, @' M; l
        Breaking the problem into smaller instances of the same problem.2 g+ j) B6 x  E) u% f

    ( L! }" M( j- {5 J    Solving the smallest instance directly (base case).
    " h) X4 f! H$ U& W8 N* ?; ?  W" ~+ G; {8 a
        Combining the results of smaller instances to solve the larger problem.: G. ?+ D6 P; L9 _5 o# `' w

    : d: q. ^! [8 X3 j! X5 ZComponents of a Recursive Function; C6 h7 @, W5 r# h& G- i
    ( T$ y3 ^) b/ N: y( p
        Base Case:
    / v  i9 n0 Z7 G4 q6 f* R1 q6 l( q6 H& m) r# H0 y9 N9 i& ?
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.4 s2 e2 H0 K6 k" k% S: q# G& |, @
    9 F& i( p) ]& X9 P5 T4 f
            It acts as the stopping condition to prevent infinite recursion.
    : X/ S0 l/ W) E7 [% }
    ; S7 K+ G" d. @+ O; B        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.  m$ K7 x' M9 k: I+ f+ b
    7 q, w7 w5 ^& F( A- i2 C
        Recursive Case:
    6 i/ w8 {" J! `( K! l2 s: K1 C+ ?; T0 K
            This is where the function calls itself with a smaller or simpler version of the problem.) c2 m, ?( t9 L) |5 B

    - q* }: e$ L; ?$ c        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).' f2 S4 U3 H$ o/ B3 z
    9 s2 U* B; A; f, Q. k
    Example: Factorial Calculation
    . k9 L3 c9 f8 w5 n
    % k' D( H" s4 _9 T) s6 j6 ~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:2 }% ]) ^5 Q" F5 G. x* h1 ?" Q4 O

    " t7 v, P4 y  [4 l    Base case: 0! = 1
      ?/ C& Q; t2 ?
    1 _. \4 T# k! z9 U8 T, `) M& M; l    Recursive case: n! = n * (n-1)!% h& s" w* k$ f* {( K' k$ B

    , V: n. b/ \8 D) q% cHere’s how it looks in code (Python):3 @+ X8 M: S" V' N
    python
    ; ]8 q* h* ^, D- B' M6 r
    ; z, N' q& C* R7 k& i! }3 Q& Z' ]9 p$ I# J
    def factorial(n):
    6 w# P' n$ a, K% _/ L    # Base case
    0 ^5 j: n' q0 x    if n == 0:
    * K8 {1 J7 E4 q, A        return 1
    7 v& O; \. F7 P    # Recursive case
    & D+ A, n8 m; i$ N    else:
    & {/ Y' e9 R6 B. I        return n * factorial(n - 1)
    + o3 r: t9 |* ^: y+ q% W% c! K
    9 ]# V3 X8 O7 @* R# Example usage! U/ ^, F& k: N8 G1 S- A
    print(factorial(5))  # Output: 120
    + I# _; |( u& V' t3 [1 P: p# b( F/ T. j, P# e
    How Recursion Works
    0 Y; B% s3 m. p8 \. M% P6 Q" z" p0 M" t2 K% @$ Y$ _3 s1 l7 H
        The function keeps calling itself with smaller inputs until it reaches the base case.
    / S# h% M% [  e" }+ V( d4 }
    4 g, I+ ^& e  X' A    Once the base case is reached, the function starts returning values back up the call stack.
    0 f; u3 z2 J! I. ^# i  g- T% k4 ]6 T
        These returned values are combined to produce the final result.; F: n4 ~5 G; p- A$ W# N7 O) U
    0 D. Q$ t9 d, r2 C
    For factorial(5):. g4 K6 R0 m, i: R( H, G3 m2 B" i

    4 Q% W6 W  C4 Y2 k! T1 S$ W9 R( t+ n' o
    factorial(5) = 5 * factorial(4)
    7 M# ^# Z: l0 P  y+ bfactorial(4) = 4 * factorial(3)/ B3 J, I5 G0 H' B8 C* ], B
    factorial(3) = 3 * factorial(2)
    + P5 y; G1 ]5 D. Vfactorial(2) = 2 * factorial(1)
    " ?9 A4 d8 H4 lfactorial(1) = 1 * factorial(0)
    0 z* N# x# S9 ^6 j) U. W$ K+ Gfactorial(0) = 1  # Base case6 u1 o# i: o: G, ]/ b4 {) z5 C

    $ G. ]( ?0 m9 ]; L9 \0 J3 h5 ZThen, the results are combined:
    0 B. X! h1 R6 J2 I' O( k
    , C# j& o' o" h: q2 k+ S
    7 f, e, k/ Q+ x/ G, s6 v  _% bfactorial(1) = 1 * 1 = 14 _* o: T! T3 u4 \
    factorial(2) = 2 * 1 = 21 X, B  o4 ?1 \4 T  |) d0 K
    factorial(3) = 3 * 2 = 6
    ! t* ^) g  Z' d0 K9 T2 Wfactorial(4) = 4 * 6 = 243 M8 p# U/ s! n
    factorial(5) = 5 * 24 = 120
    ) t, Y- ]7 b; y9 a4 @2 z2 ^' @- F* V8 Y; g  ?3 T+ U6 B0 a, F
    Advantages of Recursion0 I) s: e0 @0 R9 I4 S  {
    6 ?; g+ p" {$ u5 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).# X1 R) x7 D, I, I4 a
    4 p6 n3 \! c6 h/ }4 _+ r
        Readability: Recursive code can be more readable and concise compared to iterative solutions.$ e6 b2 N4 ]2 r# v

    $ \6 P  \- G3 _& u6 t4 N0 a# H. FDisadvantages of Recursion
    $ ^3 E* G! r" P" d& T
      k$ Y& i* i" d% ]    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.
    3 ^3 C1 N! _# I% R3 y- s- P. W2 f
    , E( W- }: A0 r    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    4 {4 f0 O& {) o) t* H
    2 I$ `' D' b, [+ G0 d& sWhen to Use Recursion1 \! z) z8 Q3 V) W4 M- `

    ; J. |: Z" Q' K4 h8 t    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).5 W: \8 J7 K4 ?! m
    ' q: B0 z7 y4 v# p6 I' m1 |
        Problems with a clear base case and recursive case.* i( p% U2 c4 z1 x2 c9 U( T

    # m! A3 l: P5 v9 AExample: Fibonacci Sequence+ l0 w; G) I5 O2 f# h
    $ p( x" Y1 F5 v/ ~
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    , s/ x/ Z/ B- N) s& R2 I8 y# l: H" q+ f! e5 x7 p; p% J
        Base case: fib(0) = 0, fib(1) = 10 V# R0 W1 c% n* m. m

    ! m5 l" P# l3 n. f/ V    Recursive case: fib(n) = fib(n-1) + fib(n-2)" b7 i$ G7 K; T" u/ D
    # [9 C) a. @# x; J4 N" ~$ |
    python
    4 V( ]; ~9 C' f  D4 U/ ]0 `" v$ w0 M6 l& e( C$ }& x7 [+ F1 J" M) v" M
    . G* V/ s. B* K) M3 v1 I
    def fibonacci(n):' q, s  j% l# B  s7 g; s6 v
        # Base cases
    1 E: N& k. b0 R8 l0 J) a1 t    if n == 0:+ a( _: C8 m6 ^( Q
            return 00 _% Q9 A8 W/ ^0 B7 J4 _) R! y
        elif n == 1:
    $ a. P9 N4 ~- g2 K! T        return 1! D! B, s7 k/ s% K6 _/ J. K  T9 z
        # Recursive case' ]$ \( m2 E( u  O0 a6 r
        else:# j3 w8 @0 S' p; L
            return fibonacci(n - 1) + fibonacci(n - 2)# `' }/ \# b8 U4 e

    5 z& r( z* ]! p2 {# Y( t# Example usage$ e1 z9 b0 o% {
    print(fibonacci(6))  # Output: 8
    8 ]* [( h% g8 K# [5 g4 ]( f7 \: k3 f1 b! H; E6 b+ p1 Q
    Tail Recursion
    + T! m# X1 x4 J* Z0 x6 R2 ~+ l& X) F
    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).
    8 E+ A3 }- V5 _$ `9 F$ c7 a1 h/ v( M5 _$ W8 b2 q
    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-21 09:07 , Processed in 0.062186 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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