设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 - S1 ?6 N! H/ X! Z) P' ^( S

    # s) M% y* Y% a. {" l& @  G解释的不错
    - F$ i! D) N$ r1 S$ Y3 U
    1 }( h- r$ w7 h7 F递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    : q0 g9 c8 B0 G; v1 o3 O4 L. O8 B9 g4 a* v
    关键要素8 t# X$ V9 A1 i9 C3 {: s% r
    1. **基线条件(Base Case)**4 x! I  R; C% C5 F7 f! [. P
       - 递归终止的条件,防止无限循环
    & ]; v7 \9 K, P% n0 G2 N   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1. u. K3 [  S# N$ P& y
    # q( \  c( K% {# E" `9 l4 e0 p
    2. **递归条件(Recursive Case)**8 u) o" X( d7 S9 \+ F. \
       - 将原问题分解为更小的子问题
    2 j% d) G% n) @   - 例如:n! = n × (n-1)!6 v% c0 @; [7 b9 _0 u

    3 j. A( p7 b, N- f 经典示例:计算阶乘
    ) e2 ~5 ~( J+ a9 s2 A- e; B3 Mpython
    5 C5 D) R# |- {$ P& W) h9 Z- gdef factorial(n):1 O* ?" s+ u& Y1 f
        if n == 0:        # 基线条件1 g8 ?+ B. X& C2 ^8 e4 }
            return 1- u/ ]( q7 r8 J2 p. N, L  O  s
        else:             # 递归条件6 q0 r7 ~# Z( D
            return n * factorial(n-1)
    2 `# V! k- O% T) ]9 i; |1 [执行过程(以计算 3! 为例):
    6 R$ i6 E2 Y/ ^5 L5 Z  sfactorial(3)
    2 E0 x1 d, b& e/ C3 * factorial(2)* Q6 D2 [$ r8 _0 S. i0 L1 W
    3 * (2 * factorial(1))5 g! [" f0 z% f/ u. k
    3 * (2 * (1 * factorial(0)))1 V0 Y: Z. l3 X, e9 l% I6 {
    3 * (2 * (1 * 1)) = 6
    ! I" f. ?# y" C8 P0 }
    ; F' e$ R) s! x1 j4 W9 u4 d 递归思维要点
    ) H* E) J* C% w6 C1. **信任递归**:假设子问题已经解决,专注当前层逻辑0 k# T5 z' v# h( Z6 v5 J
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    , I5 J8 N) ~2 e3 D' b: n7 }3. **递推过程**:不断向下分解问题(递)
    9 `& A4 e) l  r: h) H6 w' O3 i) A4. **回溯过程**:组合子问题结果返回(归)
    / X+ |0 d7 B5 i/ L& p: U0 C* G6 K- \
    / D% P2 l) [# o+ A, C0 ^ 注意事项2 e. p/ o; n/ ?
    必须要有终止条件% k/ B9 S3 C1 C: ]' a8 b2 W
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    6 T9 ~! O) [/ u& B- P$ D' N某些问题用递归更直观(如树遍历),但效率可能不如迭代
    ' j  X! N$ u6 c% u1 u尾递归优化可以提升效率(但Python不支持)
    * N# e3 f  E3 _' B2 S* s
    ) V5 V$ B4 B5 s3 u  k& v 递归 vs 迭代1 ?- J0 H3 O+ Y, k# F+ m5 h
    |          | 递归                          | 迭代               |. `- q& g  Q' v+ i1 S8 ~
    |----------|-----------------------------|------------------|5 }! ?% ~4 d) Z9 ^9 ~/ C- p
    | 实现方式    | 函数自调用                        | 循环结构            |7 T0 ~; N( b' l1 ~
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    4 ]7 Q& F0 e" }" ]# h  j! h| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    & @- i3 X8 }% j9 k  d; d8 S5 Z8 L| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    % o3 Z# p* A& \$ h3 Q2 D$ v; ^/ D) S
    / E( ]  q$ R4 T* O0 \6 r+ h 经典递归应用场景, I6 w6 z- @( t) e$ s# F9 i- b* I
    1. 文件系统遍历(目录树结构)
    : a; u- ]' S# W# H: _. K/ n2. 快速排序/归并排序算法: E9 t3 R6 M# l0 z
    3. 汉诺塔问题
    % t) f% A; H( G* f* D1 C/ Z4. 二叉树遍历(前序/中序/后序): E" N( v9 h$ o* g
    5. 生成所有可能的组合(回溯算法)2 M, h; l* P; }% h
    ' ~+ B8 U) Z& m2 t; Y
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    奋斗
    昨天 06:39
  • 签到天数: 3105 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    : Y0 w" K& y4 l/ D3 Y& R我推理机的核心算法应该是二叉树遍历的变种。1 H4 _% k/ e, i1 `! K
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    ; U0 ]6 `# Z- o& G& aKey Idea of Recursion2 U4 B% W/ |" O
    ' c2 }( i* W1 e5 O" V
    A recursive function solves a problem by:
    7 q: E, j! U, v; V0 C5 i1 P$ H" @8 b
        Breaking the problem into smaller instances of the same problem.5 p. n% u+ c* H/ \1 s
    4 T- D6 U- R$ f1 e, q
        Solving the smallest instance directly (base case).: N/ e7 x3 L: M) u
    3 N1 [6 V" ^+ c7 S" W7 S
        Combining the results of smaller instances to solve the larger problem.
    9 N3 W8 g& Y0 I% A: d6 Y* i# B6 m( c9 z
    Components of a Recursive Function
    & C" r8 f& C6 A3 I+ e# J% j
    7 V! V) s! G3 t+ K: a    Base Case:
    ' U' S: @2 V, E% `
    - w8 W# f) ^* [9 ]        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    ( F9 |' a; q8 E2 i8 M( [# M" E$ i7 P/ G4 e2 [
            It acts as the stopping condition to prevent infinite recursion.
    4 W1 V+ z+ ]6 P& T( ~3 R, h
    * U3 T! z1 C, n- c( h; Y        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.5 \/ r+ i9 C* i5 f. q

    5 Z/ _$ S7 t- f; {  ?    Recursive Case:
    " W3 T" f, m( L" W$ P' b9 Z
    6 L% m8 h4 w$ y        This is where the function calls itself with a smaller or simpler version of the problem.% M$ z+ K' j8 j
    / N# N2 z$ g9 }8 |& ]7 O) E
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    # Q( i$ V* @8 }$ P7 v3 G- n" \+ b* _2 l' X& i
    Example: Factorial Calculation
    - U' T. U  P7 N) T+ }. s$ U  ~4 D* K+ P% J4 p2 _' w! o
    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:* o; J- I7 p$ f

    ( N( ~( C+ Y2 |0 ]/ ~& D2 c    Base case: 0! = 1
    3 Z& M( e0 w: t& C- M) J
    5 }/ C1 g( B$ j& h" K7 q    Recursive case: n! = n * (n-1)!! s$ j# e6 [! ?9 \7 D1 w

    5 g4 R3 i8 m$ X8 j6 ^Here’s how it looks in code (Python):: e- k8 r' v" }0 k3 t! p! [4 Y
    python
    4 X  ~! ?- t/ i5 t4 L/ b
    ) D9 g  k! ~( f; t1 W8 _# l) ]3 e, ]9 B- b
    def factorial(n):
    + h) ^2 `4 S. @$ s" r  b4 C    # Base case
    # A) y  \2 Z6 j7 R0 W    if n == 0:
    # d- e5 G9 ~5 N5 I3 `0 C# W) A1 m7 b+ J        return 1# I# Y9 M, R, g' ~/ Y* {
        # Recursive case/ |. ]  |$ j: k( a" X/ `$ j4 j+ S
        else:
    2 w4 u, I9 x4 i  y        return n * factorial(n - 1); x' _1 O5 q' n6 ?8 e% W5 e

    7 t* [1 a/ c9 q1 n- E# Example usage3 |- h0 o# K3 R8 |1 X9 l: W0 o
    print(factorial(5))  # Output: 120
    ) O& B; u! ?+ k5 A  Y9 _  m5 B. e/ y6 m. @) a" e* q8 h8 T( s: I- O; ?$ R( w, u
    How Recursion Works. K  K- w& x. k; z

    % G% c5 r) ]) y" W* @# a: x6 ?    The function keeps calling itself with smaller inputs until it reaches the base case.
    3 [( p5 j) Z  f2 G3 z
    ; N6 o" t" T# o, L    Once the base case is reached, the function starts returning values back up the call stack.) e" l$ V) ?  l+ }8 x

    * i0 V* Z& {6 u, b. U% }" j& y! @    These returned values are combined to produce the final result.& l( S# J9 w  s* b

    1 R* |  E# T1 F3 b; @: Z' @For factorial(5):* t$ h! C& \$ M& x0 U# i3 K5 t+ p
    . w9 Q" O. x( J9 P+ |; H

      C# c2 `: I/ k5 _, {2 M9 x. Rfactorial(5) = 5 * factorial(4), O! o. i, U, Q& Y* `
    factorial(4) = 4 * factorial(3)8 q; H2 }: {2 ~- a+ ^) X$ ]
    factorial(3) = 3 * factorial(2)" ^0 p% S2 v6 m( l$ \& K
    factorial(2) = 2 * factorial(1)5 M/ G2 |: o' @( @# Y- ]! @
    factorial(1) = 1 * factorial(0)
    . e2 r+ }! t1 v0 a8 b% Ffactorial(0) = 1  # Base case4 \' m: I9 @; g

    + q8 t- W7 e% }7 `" f+ N/ oThen, the results are combined:5 G- [/ ]4 P. f, W
    9 I* C1 h. w+ T6 ?' r+ B0 z/ m
    2 _. w# I- y9 {) D8 z
    factorial(1) = 1 * 1 = 1' T; {7 j% n5 J
    factorial(2) = 2 * 1 = 28 @. Q$ d2 \/ H$ c* V0 [, W
    factorial(3) = 3 * 2 = 6
    5 d" m5 P$ e5 Rfactorial(4) = 4 * 6 = 248 y! w+ y8 t- T- \5 `& b- h
    factorial(5) = 5 * 24 = 120) ?6 c& t6 @' y( f
    " B1 d9 }. v* h, Z! m; O
    Advantages of Recursion
    2 J! R2 H  n& p! E1 f
    " j8 ?2 p. o" B5 V    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).  I( l4 k0 f2 N- d+ w: l1 X( ^
    + i& F6 W5 |+ V, S% V& ~7 d
        Readability: Recursive code can be more readable and concise compared to iterative solutions.+ H, A+ u% G# W1 y/ ?- v! A

    ; K8 |, b0 f/ S/ A; V- L7 X2 w3 KDisadvantages of Recursion4 f, F) B. G  `) }  O) S

    5 P( ~' H+ F  M/ M. D+ z    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.
    8 D: I* k6 r, U3 h" }3 e- p9 W
    + B) t0 ?5 t+ V1 {    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    ' c5 ]  M: S" t. |8 Q- R: H! ]; t1 h: q, a1 E' z5 a
    When to Use Recursion& `: Z- U& R, ?: Z
    0 s2 u2 z$ Q6 j$ l5 _8 F- A' m
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    6 p( J0 J+ y* R' `0 D( i' S* k# M- p, ?: Y( R3 P
        Problems with a clear base case and recursive case.6 }$ o2 S( A2 P7 z3 h) v$ `9 d  i  g

    ' @5 Q: G9 S+ u$ g, g9 i# `+ VExample: Fibonacci Sequence
    . ~5 @; h3 f. k% D8 G' F$ _% ?6 z+ V# k% U2 s
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    : t+ i  M6 H8 j# G" C" K
    0 J. u1 }+ ^8 K" j  H    Base case: fib(0) = 0, fib(1) = 1) U2 g" t# _4 z5 G8 R
    $ s& Z! O- w& V( N) `, O! j
        Recursive case: fib(n) = fib(n-1) + fib(n-2). o$ R+ s' n: T

    ( g# x4 _& R  D1 u( F& V2 ppython
    * j' ?  U* ~( k1 s- n3 q/ ~0 [- {  D1 D: m3 q5 m
    . R. x+ N; x. ^' B! r" X
    def fibonacci(n):
    5 i5 l& E1 A0 d9 p* O$ Y    # Base cases/ C: ^/ h" |- |0 B
        if n == 0:# z+ u% t. w2 @# Q; P
            return 0' P6 }3 l/ `) k& f
        elif n == 1:2 A2 Z$ c; a: e  ?# U: {& B7 l0 K
            return 1
    2 Q) w' p! d" w1 R3 Q, l' [5 P    # Recursive case* c) m3 ]# v+ k
        else:* p  I- i1 |% ~9 C3 k, E; J3 C
            return fibonacci(n - 1) + fibonacci(n - 2)4 z; ^) ~9 ?% z( p( K. ]

    % ~/ w2 D1 }9 b# z+ _* O# Example usage2 q1 [0 Q7 X9 Q" d  N7 i5 B8 |
    print(fibonacci(6))  # Output: 8& U3 Z! T& t6 D4 t" @3 J* Q

    + \( Q. M4 c. i4 zTail Recursion& u+ |8 B% M+ L3 T& X) q) v

    6 b0 e+ b- q2 B& s/ ~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).
    " [/ d& ]3 y* _  Z, D
    , P3 D( ~: Z/ ~4 aIn 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-1 10:29 , Processed in 0.031701 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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