设为首页收藏本站

爱吱声

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

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

[复制链接]
  • TA的每日心情
    开心
    2015-11-30 11:11
  • 签到天数: 2 天

    [LV.1]炼气

    跳转到指定楼层
    楼主
     楼主| 发表于 5 天前 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    % t5 W9 J' q1 ^5 g9 y5 S2 _* t0 i
      L: d/ g% X! O解释的不错
    4 G& F8 l" d! ]) F- i2 U5 n& K5 m' p5 h& v7 O! T
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    6 |+ Q' K% \6 h; y( i; T3 y' T! P# r, Q$ R: Z
    关键要素
    1 F* O$ W( Y( ^9 Z: |4 a  {1. **基线条件(Base Case)**
    & o) l( }' t5 V* T2 a   - 递归终止的条件,防止无限循环) q8 o$ {2 f9 k& H% e* d8 P
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1' h( V4 z2 e/ s- p
    5 O. S8 y. u+ k1 S( o9 ?
    2. **递归条件(Recursive Case)**; |( c& y1 Y/ u- V& v
       - 将原问题分解为更小的子问题
    9 b0 |" R2 W& g( m   - 例如:n! = n × (n-1)!
    ; G# {/ A9 r# {" n$ \) N0 d* Y, [3 l: |  |$ T+ X% j" |
    经典示例:计算阶乘
    % ]' j( q1 E! l* Q' U$ ipython
    ; X  l$ h: P; n* d# Vdef factorial(n):
    . Y; W4 q7 E- k' K3 ~$ n    if n == 0:        # 基线条件' G! K0 t: o4 i; {
            return 1& {/ I3 G) W& i1 ~# z9 d  C/ Z, E
        else:             # 递归条件; ?& z; D/ H% x) j& S
            return n * factorial(n-1)5 |7 u6 ~0 E8 p) g$ U
    执行过程(以计算 3! 为例):9 e7 v5 D: x! z: ?
    factorial(3)
    7 A) M' p. E5 W1 j' b4 F1 b3 * factorial(2)
    0 k& x  \0 X; z8 {3 * (2 * factorial(1))
    1 b0 C  k% m. l4 K, U: f% ]  G3 * (2 * (1 * factorial(0))). w" E1 u+ p. O
    3 * (2 * (1 * 1)) = 6
    ) H7 Z* K# `' Y! N
    8 D& J  d4 x' b0 f8 l6 h) X! L 递归思维要点
    ! @7 O  N( N6 q4 r$ U8 X* m1. **信任递归**:假设子问题已经解决,专注当前层逻辑5 N  z. n- |& H; T
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    : G& W: t1 I* }! O- H8 S3. **递推过程**:不断向下分解问题(递)
    % F9 T  K% Q0 @! L1 c4. **回溯过程**:组合子问题结果返回(归)
    ! y0 W6 m. @8 O
    : {1 g# q. B- J* Z! a; N% \& O 注意事项
    . a1 P" ?9 l; R2 |必须要有终止条件
    , A: U1 C8 X2 _% J, t递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    & O! R' ]8 _- N+ d某些问题用递归更直观(如树遍历),但效率可能不如迭代
    1 L4 [: Y  m/ c: a尾递归优化可以提升效率(但Python不支持)/ I  t* p: r. v' B% i6 @& V  @

    1 R9 ]3 j$ D+ j7 L9 D. n 递归 vs 迭代
    + c# k5 w# [1 |# }+ \! n|          | 递归                          | 迭代               |0 ]+ {0 Z  K1 s8 K0 G
    |----------|-----------------------------|------------------|
    1 }  h: ^. K* G/ M7 e| 实现方式    | 函数自调用                        | 循环结构            |( t. t3 X* s" t/ x
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    6 z: {+ @5 [* y% X# x| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    $ W/ E8 R) H5 [% ~, n. ^" q7 ~7 ^| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    + i3 V- w9 {3 S1 l" U  s' o/ Y  u1 y+ p3 Q/ x5 U2 n  D
    经典递归应用场景) i% f; Z# b  s, t$ H
    1. 文件系统遍历(目录树结构)
    8 Y8 u$ t2 x* E" y* @  ~2. 快速排序/归并排序算法" J, C0 @! W7 ~9 s- }
    3. 汉诺塔问题( o5 A( b$ F; Z0 ?: k& T
    4. 二叉树遍历(前序/中序/后序)$ v) }8 a. c# F; \
    5. 生成所有可能的组合(回溯算法)
      y8 J4 j' g, L, |2 E4 D* _6 I. l& |
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    郁闷
    2 小时前
  • 签到天数: 2821 天

    [LV.Master]无

    沙发
    发表于 4 天前 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    8 H. \0 ~6 K7 S* t1 X' E我推理机的核心算法应该是二叉树遍历的变种。
    ( P) a  @0 N2 J2 C( q! ^6 z% Q1 u另外知识系统的推理机搜索深度(递归深度)并不长,没有超过10层的,如果输入变量多的话,搜索宽度很大,但对那时的286-386DOS系统,计算压力也不算大。
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    板凳
    发表于 昨天 00:45 | 只看该作者
    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:$ B& [* v& ?2 E  t9 E2 }
    Key Idea of Recursion, K. e  P; r1 D. ^
    % q8 c; }& J0 c  \2 k* ?" @
    A recursive function solves a problem by:
    ! n' P6 D( B( k  j1 G1 P. m8 g2 V# t: u! ^9 b
        Breaking the problem into smaller instances of the same problem.; Q( X, G# S) V8 T
    0 q9 c  v. N5 O0 |# q
        Solving the smallest instance directly (base case).8 y  `: s9 ?! s: z0 h- ~7 \, W8 l

    2 E/ v9 b/ y! E9 j! s( Q+ F    Combining the results of smaller instances to solve the larger problem.
    ) I4 E8 Y+ {) h0 z* t' n
    - Q2 T, x7 W, \- W2 LComponents of a Recursive Function
    9 d( G' P# Y% ~6 D2 g( C/ ~& S; \5 p. h/ ^5 V
        Base Case:) A$ @0 M" [# ~; g% i7 ?' W$ d9 Y3 o

    1 X- A' x2 ^. D8 t        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.: n, t/ F! E" O
    % ^4 Y$ `! Q" |. g' u
            It acts as the stopping condition to prevent infinite recursion.: o3 M& G5 B* J8 S: \

    ' t, N0 z& `& Z8 [  x+ T" Q+ L1 f        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    8 D' g, x- _  D% ?. }, D6 A
    6 Q4 R- q9 l* U* M6 G    Recursive Case:
    3 [) ]) y) p% o2 p; f3 R; Z# ~6 u8 w* A6 V: U1 A8 E1 P7 o  d/ a
            This is where the function calls itself with a smaller or simpler version of the problem.% u( |6 j2 S; O1 c

    % C5 ~1 X  w8 P9 l; ^6 ~        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    ) x( M* I( G2 B6 \% l# B. N
    ) N! `- Y5 v  e. w' ^9 cExample: Factorial Calculation/ f# ]6 R; H" {4 O; g
    3 ~* W5 z' b& m& B  P
    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:
    ( t) v$ Y! e1 e  \4 d' V0 ^3 s" Y/ D5 D+ K
    / \- l  K  [% _8 P! I    Base case: 0! = 1
    ! }: u1 ~* t9 @7 \! k9 C) s* Y6 V1 n' a$ V1 A
        Recursive case: n! = n * (n-1)!
    + m( f8 T3 Z  _+ s4 h6 G
    0 h5 j, k+ P% K- ?# HHere’s how it looks in code (Python):
    6 A- q7 ~& ]: |4 G& d7 Xpython
    $ J# T; B+ U' ^4 P
    $ U, w- D1 s  c5 E: ~0 V  [* F8 a+ w" V2 ?0 o1 d
    def factorial(n):& z/ }/ k% k1 j; R2 @# S. I6 _
        # Base case4 c0 q, _- G" b
        if n == 0:
    * K: R  Z5 s2 m6 X        return 16 c9 D  l/ A0 P' M% f; s
        # Recursive case
    9 T( A" P( ^* [: q) R0 r3 F- ~' \9 j    else:
    . _  v( l5 X. a& \6 S# Q* L        return n * factorial(n - 1)/ a9 y* |$ g6 Z7 I+ Q
    / P# d. q# e0 U5 ~1 O' g
    # Example usage
    1 z1 r6 d' c% k% g, k) q% [print(factorial(5))  # Output: 120
      G, H+ y% ?( H
    ! [% a1 {' {6 _7 P0 C5 Z% tHow Recursion Works" V* c! ]% {  ]! L2 _

    1 t& H5 R+ m2 I/ G    The function keeps calling itself with smaller inputs until it reaches the base case.0 t6 T9 c$ H4 j4 J" V7 ]
    ! \  U. E0 v0 {( ~
        Once the base case is reached, the function starts returning values back up the call stack.
    6 z( [3 J  t( E! |+ F8 s+ ]" N# w( w# a1 [  E% Q1 ~6 @" L
        These returned values are combined to produce the final result.
    ' T& g8 Y' q) L6 s  D
    + U8 J  x5 D/ b/ jFor factorial(5):" _* q% f6 V! T' B$ c3 p# \
    1 C- G- v3 @" O# d) Y5 N6 u: S

    " |* b' P8 V* h6 P# J8 C2 f$ |( Wfactorial(5) = 5 * factorial(4)/ ]( r0 ]6 \4 Z5 g) \3 e( B
    factorial(4) = 4 * factorial(3)
    7 q5 d4 X+ l% E% i7 ifactorial(3) = 3 * factorial(2)
      n0 G3 a& j" u  Ifactorial(2) = 2 * factorial(1)
      H" @0 O$ |0 W, k- _factorial(1) = 1 * factorial(0)
      o( ]" f& N2 S; \4 Bfactorial(0) = 1  # Base case
    7 C/ A+ U. i+ E" H
    3 ~" j, I( J5 {' b9 a( AThen, the results are combined:
    " P4 T& [/ w* t! d# d2 {* T
    6 k+ {* o# q$ ]2 r# b+ t
    ' z6 A* y) I) E7 S. t: P% l1 ?factorial(1) = 1 * 1 = 1
    3 Z1 p8 h6 u: `factorial(2) = 2 * 1 = 2
    ) O& F7 ~2 Q( c& I3 \# Vfactorial(3) = 3 * 2 = 6
    ! m7 H6 U. b) |; d! Vfactorial(4) = 4 * 6 = 24
    6 `8 j' g" c6 \" \1 ofactorial(5) = 5 * 24 = 120. ?0 x' K5 _; U. O; ~

    , l( R, `! [" k% }, yAdvantages of Recursion+ |7 o9 ~$ B+ Z0 t2 j5 w, J8 ^

    # f, l$ s5 O2 v% @/ T    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).
    ( |) @$ ]% U* l1 x4 M: h  S: v7 K0 y+ ]% N) W' h2 G
        Readability: Recursive code can be more readable and concise compared to iterative solutions.2 O' m7 m( ~- ?4 G6 N" o

    0 q& i+ ?, ?5 r: G* [  GDisadvantages of Recursion7 [. j, }  u  f( s  y& k' X

    9 k  t3 n$ n- e    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.) x0 r  b! P+ v) z

    ( j! k1 q0 _$ f. E: Z( x    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).: Z7 q0 G4 r9 i+ ]9 v8 B2 T/ c! b% \

    , ]/ G0 g, g6 M  g! }' K7 k1 r* ]2 GWhen to Use Recursion7 U/ H2 N: T0 J( t

    : X2 V( j0 g, ?; d0 c# }    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).# t1 M* t& T1 D

    % H/ T  I9 l- N- X% d9 C    Problems with a clear base case and recursive case.
    $ K0 |8 V( R* O
    1 U: d* U! R6 l$ A2 n; {8 y- BExample: Fibonacci Sequence8 P8 A; a% R  Q8 ^
    1 O* p' A, u2 E9 w
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    7 e( L7 g& N0 U0 O
    ( n8 h- S" c8 P5 ~    Base case: fib(0) = 0, fib(1) = 1' Q8 L" w& A0 ^) m2 t2 |
    ) ], j* e7 B0 f0 z( ~
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    9 n/ A. x" X7 l: h; f3 v$ T
    / C  X/ u' A+ \) z4 k# `' K0 Mpython
    % `. _9 D, y( f1 @% X4 v, @
    $ l3 Z, h$ L2 u- j1 E) }7 m5 f/ B2 ^( g/ J4 N: Z  O1 j
    def fibonacci(n):
    ; E* @. a6 P% R, r5 q    # Base cases
    3 @' D- u( v( W* [. }$ I, B    if n == 0:0 a0 n! c2 G$ b* l/ ?+ ]/ M
            return 0. j3 ?9 u" e1 Y2 U
        elif n == 1:
    & W1 J6 ?0 V, w3 b# m        return 1
      I4 C) Y: n5 Z0 F) Z$ I    # Recursive case
    & t! h/ v' m1 \    else:- M- I/ h/ T) Q( t' w' F5 q0 H: o
            return fibonacci(n - 1) + fibonacci(n - 2); w" x; ~1 p* D* s7 d. E* {8 B! h

    ) }5 q' v# c% `/ U: ?# Example usage
    $ q- w+ B% Y1 h- U! Y/ jprint(fibonacci(6))  # Output: 8* ]9 u* }0 ^" G' v
    $ C# M% x/ S0 x- p4 g
    Tail Recursion
    " m1 ]4 |: t- F+ \# [
    , l7 w3 k, R) X& A( \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).5 @9 f  X. x  [  s

    ; Z$ f" t1 a- q  _3 \$ 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.
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    地板
    发表于 昨天 00:47 | 只看该作者
    我还让Deepseek 给我讲讲Linux Kernel Driver 现在的开发流程,让一个老同志复习复习,快忘光了。
    回复 支持 反对

    使用道具 举报

    手机版|小黑屋|Archiver|网站错误报告|爱吱声   

    GMT+8, 2025-2-3 04:48 , Processed in 0.034159 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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