设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 7 d% l5 Z2 I8 R" h) [4 W
    8 E% ~7 W' |& M
    解释的不错3 Q1 s/ G' e7 u- ?( Z0 P  W
    5 i4 m/ F" H2 X- t1 E
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。7 E% G2 ~/ S/ c$ }# H- d
    9 ]0 y# K1 _0 d+ ?- G8 k- Z+ P4 n
    关键要素- q. L$ V. }' U* L3 t
    1. **基线条件(Base Case)**
    # a: n3 B+ C" N1 r   - 递归终止的条件,防止无限循环
    - Y8 x! W0 Q% w* A  d   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    ' A% e7 I% C! b* F$ U' K; q0 l( H0 c; ~  G# V% u) |# O+ S) P
    2. **递归条件(Recursive Case)**0 v: s7 H, ?/ O/ V, c& Y5 Z- d1 L
       - 将原问题分解为更小的子问题1 b! t" h9 K# p+ L
       - 例如:n! = n × (n-1)!. t$ O! I$ b7 f# x' b. f# E- l

    / _8 u3 a0 ?! ^6 T9 ^* R) t( B 经典示例:计算阶乘6 v! @2 ^- t5 w$ D
    python3 K8 I# }2 s( ?1 p
    def factorial(n):9 s9 A2 n( k8 |8 h% O4 O: f4 b3 h. S
        if n == 0:        # 基线条件
    7 q% R6 t: O. v) @        return 14 `9 e5 s5 n, o
        else:             # 递归条件
    - }/ Q* G3 h5 V- B  a        return n * factorial(n-1)
    6 n) I1 _; W* ^1 X- L1 Y执行过程(以计算 3! 为例):
    0 J" w, I# i8 V3 {8 r5 e' q/ T; gfactorial(3)
    9 m+ i7 q: o; d# `- X1 m* U3 * factorial(2)
    8 G$ o& [& V" o6 T0 I5 a- C$ I3 * (2 * factorial(1))
      Z9 x: N. R- m0 @# \2 ~3 * (2 * (1 * factorial(0))): T, b" D/ d- s8 ]' O0 c
    3 * (2 * (1 * 1)) = 6. e; k% W- B  v. N/ z
    * C. n; m  t' a$ I2 Q. Z
    递归思维要点
    2 J4 s" O' x1 X) [' p4 W1. **信任递归**:假设子问题已经解决,专注当前层逻辑1 R; u4 q1 {# h- ^) h0 D
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    ; h& @& X% I6 g+ A6 j3. **递推过程**:不断向下分解问题(递)) w% r5 t8 v0 l- |# Z: ^
    4. **回溯过程**:组合子问题结果返回(归)0 @- W9 p  g9 w2 Y1 P; z
    : u- g& S, y* _1 m
    注意事项2 |" o, T! x; q  b: K/ G* W; K, m( w- m( B
    必须要有终止条件4 ]( z3 Q$ _( g! u9 P+ {
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)$ C  M9 e; R/ Z. ~% i1 f  U
    某些问题用递归更直观(如树遍历),但效率可能不如迭代, H$ H& B, v+ c2 I" F
    尾递归优化可以提升效率(但Python不支持)
    - y3 I1 A$ O# F; b/ y% ^/ I5 U9 N( O4 K1 O+ U
    递归 vs 迭代3 A& v  ]5 I8 T8 ~" `# ]6 ]5 `+ i7 v
    |          | 递归                          | 迭代               |
    " ^7 |0 x7 i) D( ?|----------|-----------------------------|------------------|
    + u: ]4 E; Z8 K5 Y, V5 M" i, o| 实现方式    | 函数自调用                        | 循环结构            |! F( }1 |; F  B3 M, H. o5 _
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |8 t6 m, x" N: P$ c# Y
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    2 X; n# X' C& i+ v) k  p+ @! i4 F| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    6 c; U& {$ |7 f. r: ^
    & M/ T  _( N3 s1 D) Q) J 经典递归应用场景
    , D2 V2 P9 l2 C, P/ z4 _1. 文件系统遍历(目录树结构)& p" Q( x& e$ H$ I4 r7 u
    2. 快速排序/归并排序算法& E& w( [: F- I& J
    3. 汉诺塔问题8 Z) n& i+ }+ O
    4. 二叉树遍历(前序/中序/后序)/ i0 i9 c- V, e& E
    5. 生成所有可能的组合(回溯算法)% P0 V% X, t) q. b+ x8 E3 P

    # S* Y1 V- @3 Z. u) a: h* [试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    无聊
    前天 06:43
  • 签到天数: 3101 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,5 w$ r8 \2 J  o+ q) i  l  p
    我推理机的核心算法应该是二叉树遍历的变种。
    : h0 N, V/ X- b. B' t- c) 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:
    / I. F' f1 n& WKey Idea of Recursion
    ' c, |4 o: z, ~8 Q1 l5 }, J6 d/ Z& J* b( x' d3 Y
    A recursive function solves a problem by:# T8 o# Z  H+ `" g
    ( X  p, j1 y( m+ H& e
        Breaking the problem into smaller instances of the same problem.
    7 o2 n) n2 o! V5 i, K5 H+ z/ [* N7 `& S( n+ Q7 p
        Solving the smallest instance directly (base case).: ^8 D8 U$ D* l1 w7 W
    ) S# ]6 o  Z6 h. q$ `' V9 o( E
        Combining the results of smaller instances to solve the larger problem.$ c4 a$ y7 ^) B3 n) B' `
    7 {% J8 c  E4 ^3 R8 n9 p6 h
    Components of a Recursive Function
    6 H  l+ }7 k. \' f# J5 p
    ; k2 i  j: H" ~" m  r  [. j. P& O1 c$ s    Base Case:
    $ @5 L. U2 q$ F3 n( l
    & u0 }6 [; }( x) u! b: Y% y        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    ; _% g0 j! S  U1 K! r/ _. V" }5 Q8 f  D: b5 r: ?0 N
            It acts as the stopping condition to prevent infinite recursion.
    & ^4 E5 [8 L, l% s1 z9 V7 D
      z; F- h, q1 j        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.: N6 ]7 o8 T; c3 g: @2 L( ]( y; `

    6 l8 O, |: J  `4 {5 r# g    Recursive Case:
    6 D3 h6 v* c# {; L
    ' J/ b/ ~; Z6 Q: p        This is where the function calls itself with a smaller or simpler version of the problem.
    6 F5 p/ D% e: c6 {
    . b0 x" I5 a3 u4 r        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).& R' V" m# z( \( W1 o

      n  v% `  ~, }9 C- |/ z' K( wExample: Factorial Calculation- K' r' j& M5 C: h6 q: N; c
    ! m: ~/ [' Z: W8 _( w. c" W
    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 F/ S! f+ V: u" X  X
    * ^- l- @% i, G2 M. N7 K/ ]# T
        Base case: 0! = 1
    ; Q8 A$ W/ C; x. @( B6 d1 F' Z- e, W5 V) Z/ Z. i
        Recursive case: n! = n * (n-1)!/ S/ c3 W9 a$ g9 N- c# D
    - |9 K# S1 c' R" m' o. {9 |7 x
    Here’s how it looks in code (Python):( u8 A1 o# z( ]
    python4 t0 }  m, l* T$ h1 N* N
    $ ?" N8 C% ?( S) p+ d

    & E9 s( Q- e) {: U$ t, c  Q# }def factorial(n):
    # D% x$ z3 K. z    # Base case* }" P2 Y7 D) B) \; r0 F- o
        if n == 0:
    + H7 E; k. g# S3 m! e6 V        return 1; R; _; u+ j% `; A2 d8 A
        # Recursive case
    ! a0 w- X3 _8 u. E, ?' N    else:
    9 A) f; r5 J+ J' Q  S        return n * factorial(n - 1)/ r2 k. z: H- v$ i

    3 i4 }6 j% V: e; z/ @+ ?# Example usage
    ; V3 F( O- d6 p3 @6 x& I  {. R- zprint(factorial(5))  # Output: 120
    - F9 ~# a% ~- N" j# {2 a" h. s7 g( i1 Y3 l1 Z1 m$ L. j  x& x( U
    How Recursion Works
    8 u' x  b- n. o2 \4 n" ?% P- D( ]9 _) l( G" G" c# d  |1 U! D
        The function keeps calling itself with smaller inputs until it reaches the base case.
      C  m! T2 U9 M+ G+ y; C6 f$ A4 c3 @; G7 r5 T' F: S( L
        Once the base case is reached, the function starts returning values back up the call stack.
    1 \) g$ m! A& i* W  t. y+ I% T0 s  c. R3 j( e: X" e
        These returned values are combined to produce the final result.# s4 c9 d( o9 |0 C

    " Y) W, e. n- A' W4 PFor factorial(5):
    : q; s5 v. I1 Y$ ^4 S9 r
    8 t% A* r7 u) L/ q2 h0 |8 P# K& J/ V7 i  W
    factorial(5) = 5 * factorial(4)
    1 C: V3 n7 i. U, N$ C& Dfactorial(4) = 4 * factorial(3)
    ( ~1 B& f0 ^: J+ R7 Pfactorial(3) = 3 * factorial(2)
    ) |, Z& j) K5 d( l4 u0 j0 cfactorial(2) = 2 * factorial(1)
    / S, ]8 p  |! H" yfactorial(1) = 1 * factorial(0)7 K( o4 _3 |) }/ b8 c3 o# z8 L
    factorial(0) = 1  # Base case
    ; o* X7 a- \+ j: b
    7 ?- P6 V& g& |* [7 X+ T2 y5 WThen, the results are combined:
    6 O* h( O  z7 L* E. k, Z! {& A* M+ s* I0 e# b
    7 w, r: e9 t* i6 O
    factorial(1) = 1 * 1 = 1
    / Z) w$ c, x1 I0 Jfactorial(2) = 2 * 1 = 2
    % \+ `0 b' V6 C4 Vfactorial(3) = 3 * 2 = 6
    4 E  [* l  y$ |* j- E, m% J  ofactorial(4) = 4 * 6 = 24
    / |! V2 [* ~2 {) p  t5 _% a& }. xfactorial(5) = 5 * 24 = 120; ]+ x4 R$ ^8 o

    ! E; S$ H2 R* G2 s: Z2 l3 XAdvantages of Recursion! E1 p8 i$ D+ m
    # x- U+ k8 l' e5 s
        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)./ h6 v% K. j  J5 D
    1 a* z* K  h, c9 l
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    + I: B9 h" V1 M' Z8 @, a/ m8 ~" B$ ~2 [8 b+ S
    Disadvantages of Recursion
    # M- J: W5 J! D1 v) L* D
    + q( B+ X, L9 g( d7 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.
    & U1 O; b2 b; s: e  k. c
    + L$ h9 b) X4 N: c: f* O    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    + Z* N  p' ~: [' u
    & m( ~0 f# g& a. k; R& _When to Use Recursion
    2 Z( H# V- T4 H' D9 I/ q/ [9 F6 |+ S$ r+ M3 w
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).; f* R; d+ R: J8 G# V: _  \$ F
    4 k4 X* D+ b9 F" x2 q  W% d3 f* n. D
        Problems with a clear base case and recursive case.$ Y! L: U1 Q+ ^$ v+ q- ^- l9 }; B
    ! p, L7 @- _! }6 t5 a# t
    Example: Fibonacci Sequence
    8 I. P1 G  a1 Q
    % f! f5 ^( a/ X2 v& s( PThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    1 W, C# Q, }) I: G" e9 N( r0 L" E# F( }2 b! M  o
        Base case: fib(0) = 0, fib(1) = 1
    0 h- H" X- n: F: f
    0 k/ j  ]4 P( I% u  p% l, M& d    Recursive case: fib(n) = fib(n-1) + fib(n-2)) B3 e2 X* ~7 t2 F

    ; `9 f& l3 ^2 m* C7 Jpython8 Z0 Z3 \3 |) ?' a
    . v# r, D3 K. k" D, p

    + O6 n1 W, n/ ^: \0 fdef fibonacci(n):
    + g5 e& [" r0 l8 y8 `$ w$ @    # Base cases5 ?2 y0 V  T9 v) `: z+ N
        if n == 0:
    & s0 H5 A# |' u( ~1 e        return 0% s6 d$ {% Z3 C
        elif n == 1:: q  R1 G; o/ ]/ b
            return 16 b5 n( q9 X& N  X( r
        # Recursive case5 Q$ t2 c! j/ f( b  q' Q/ O
        else:# c" n( r3 M2 L( u3 A
            return fibonacci(n - 1) + fibonacci(n - 2)8 A1 m3 z. d9 c9 ]' e3 Z

    * l; o8 u# c& ~2 Y8 E# Example usage+ b- A( ~4 h; S6 F9 i# N3 f4 T( }1 l
    print(fibonacci(6))  # Output: 8
    ( z  d* ~  M+ Y0 m7 g- f4 d8 a' H7 ~  k& m6 \0 y
    Tail Recursion
    - z. [, ]1 b" L: [7 f/ J' j: r: p: |* L3 e2 {) ?0 x& _( B
    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).
    3 F  ^; d5 l2 s/ _3 a# \' Y3 a7 T
    1 F/ ]% k( {# J* ~; 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-11-26 01:22 , Processed in 0.030765 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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