设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    2 d  g; p  G$ N7 B* x$ h- [8 ^2 G5 x0 v! h6 O8 h1 A- m) A
    解释的不错
    % |8 y0 V( J6 v0 _/ Z. l' N7 \
    9 e; M# w$ E: c4 y递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。: @8 h) l" e2 d! @/ E0 C  x; w
      o0 g$ T+ D1 H) s& l
    关键要素- H7 w6 _( M9 [, q$ z2 [9 U! m
    1. **基线条件(Base Case)**
    - H  X% _" u! A4 @' a5 \   - 递归终止的条件,防止无限循环5 [! s: l* ]( h
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    ; A- ?& Q# o$ Z+ D% J. d# j: i- e6 W2 ^. s! m7 m' I5 D
    2. **递归条件(Recursive Case)**
    4 ]1 @# S- A+ C1 i8 E+ d   - 将原问题分解为更小的子问题- E: A4 Q) b; T5 |
       - 例如:n! = n × (n-1)!
    ( M1 C$ P* C+ S" j: X8 u. b6 F: a+ F( p, f' v, y
    经典示例:计算阶乘
    3 i  c8 s7 M: M# ?, kpython' L% R" K0 Y% ^3 d
    def factorial(n):
    5 x  u/ p- Y7 W; M    if n == 0:        # 基线条件9 D8 ^3 [/ @8 o7 O$ W
            return 12 N! B9 _- f) h9 u2 u6 S
        else:             # 递归条件
    % t% E3 Q& x3 Z, ^8 K0 }        return n * factorial(n-1)) i8 o1 \& A) s7 T' R! M: p
    执行过程(以计算 3! 为例):' P! e8 S& U' p4 L) B; a$ M, Y) ^
    factorial(3)$ t6 k: Z6 O- r! t& a/ T
    3 * factorial(2)
    + x2 N: a# d! k7 y8 }9 C2 K3 * (2 * factorial(1))
    1 u, |) z8 ^# L; H2 I+ S8 P3 * (2 * (1 * factorial(0)))4 H1 ?$ e/ E3 Y! g4 a; Q- U
    3 * (2 * (1 * 1)) = 6
    7 ]0 C% N7 w! S) g/ B+ m! i+ X. W. v9 U
    递归思维要点
    + w1 \( l. j" f( p# v1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    3 c) c) P: c* I& `% |' \% l2. **栈结构**:每次调用都会创建新的栈帧(内存空间)8 Z; H7 F: [* @7 ~8 `
    3. **递推过程**:不断向下分解问题(递)  M, w  l& T4 C. n5 p
    4. **回溯过程**:组合子问题结果返回(归)
    + r: K, n3 j6 m3 X& s- Y0 O6 T
    $ J' f" M* A1 X0 O/ F/ G 注意事项
    / e7 f3 `  e- l' e1 S: r必须要有终止条件
    2 g+ A: W$ F' d+ [  V递归深度过大可能导致栈溢出(Python默认递归深度约1000层)$ O( k4 O1 V# K  l, e
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    ' ?  c9 d0 z) c5 c尾递归优化可以提升效率(但Python不支持)
    % Y9 L. u: L4 m: j+ B
    4 \) Q! @3 m: U9 }# o( a 递归 vs 迭代
    ! M. k1 S1 v, N  O|          | 递归                          | 迭代               |$ U9 l/ i9 J1 G! j6 _8 [3 J& X
    |----------|-----------------------------|------------------|# d. K* V* Z# T- u; F
    | 实现方式    | 函数自调用                        | 循环结构            |
    - J+ L& Z. J- n  r4 \+ \6 M8 j( _| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    , h2 J( U, @8 }3 _4 P+ U' q7 L| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |% j! g! j( Y# K  d3 [
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |  j- q( s' y+ G/ H

    3 W2 J( C, L& H 经典递归应用场景+ h; d$ W: n' b, |. W1 `
    1. 文件系统遍历(目录树结构)3 b/ M7 c4 Q- W0 e! K8 A: |' l
    2. 快速排序/归并排序算法
    $ z/ O& E8 i" r& s3. 汉诺塔问题
    * J" }' p5 Y& i3 u' d/ `4. 二叉树遍历(前序/中序/后序)
    6 i+ a) K0 ^, o1 p& {. u9 q! g* ~1 }5. 生成所有可能的组合(回溯算法)
    6 N2 @+ d: N* v4 C5 @  O# [. c/ G! }8 f) q  Y  R6 |/ l
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

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

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    6 ]& s. q( R. m5 O7 L1 A我推理机的核心算法应该是二叉树遍历的变种。$ H2 _0 B- ^$ c5 y, F
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
      f5 F2 x7 x, q* ^7 xKey Idea of Recursion! S  \5 v+ h+ s6 Z. [# M* _
    : u  ^* ^" j6 V! @& T
    A recursive function solves a problem by:
    $ t, [# c; W" N4 D  s+ C
    . Q" u& W& E  m/ e) {2 @    Breaking the problem into smaller instances of the same problem.( a/ |  i1 I9 [8 Q" J- w/ l

    " _8 ?4 n. o5 }2 w1 R    Solving the smallest instance directly (base case).
    5 c, j# B2 l3 w& R: T
    . c, C" e7 N% @6 ]& ~4 `7 P5 v    Combining the results of smaller instances to solve the larger problem.
    6 D, o1 P9 A0 v, |3 u9 j4 }- D9 K7 C" _4 i1 P+ t
    Components of a Recursive Function% r; k  e: }& [

    ! Y9 S- O1 C% W    Base Case:! N# _! E# m  w9 i0 j) E

    " ]" f; a2 q) o8 l% c        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    ; @! o" g& I& D6 U4 B2 z; ?( c# t4 V
    ' N) H3 a- p4 H6 W; p8 s        It acts as the stopping condition to prevent infinite recursion.
    # v) e; V( a# w% j( B# v0 \# n$ w) K  i: M) Z5 ]
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.: R$ J8 u( {& n3 p' A! c

    1 A6 }/ j) @  l( s! w1 m    Recursive Case:
    # }: @3 x5 L4 V1 c- J. K# O5 ?
    6 c* d8 Z. K1 P6 t6 q# e. X7 H% D        This is where the function calls itself with a smaller or simpler version of the problem.1 y! |; c% y) y% I5 N! n$ E: F0 p

    6 i+ W4 E% k/ Y* l; T7 d        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    ; c, V2 U/ P& d5 k; k( b) ~- M2 @* ^4 R2 ~. G5 [
    Example: Factorial Calculation
    + i( A' \. e5 _* C4 z& g- B: q; e3 m% F3 d' {, S
    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:
    " ?% d# x4 \: T: s, }0 C* p! D1 V
    & g; k& A: }3 Y! Z" E; p  G6 m    Base case: 0! = 1! p+ H0 m( ~$ p# @" u
    ; K/ ~$ _& \- |6 f! `! U4 {) f' E8 o
        Recursive case: n! = n * (n-1)!4 R5 f0 w! m4 j
    . @, v1 J- h/ C8 b' ?7 w
    Here’s how it looks in code (Python):
    / Z3 m" T3 u0 r! H" @5 `9 Rpython+ ^; Z% Q2 Z& l
    2 c, |6 G+ W* m$ V; ?+ R7 C4 B3 ?

    5 E) v6 V& n3 Y8 b4 ldef factorial(n):
    3 O; L5 ~5 L' G% i    # Base case
    7 Y  s! O  S: f* L    if n == 0:
    , Z3 H' Z' x  A' b7 o" t) O. W        return 1
    ) @6 A9 n9 K+ S    # Recursive case+ q  b! \( g  J* P
        else:
    + ?* u) ~& q/ u4 D3 Y        return n * factorial(n - 1)( T& w0 O2 A" y) `

    ' f- a! ~) v5 C4 _/ ]1 L& Q/ \# Example usage
    1 t# L; O& G. @) C7 |print(factorial(5))  # Output: 120: }. y( F- L$ Q3 w4 d: e6 w8 O
    / |, E2 O& r  c6 q) c+ {1 M3 V3 d
    How Recursion Works+ v2 E( D' v+ e/ |9 m

    6 r2 z; d, N+ k5 h1 Y    The function keeps calling itself with smaller inputs until it reaches the base case./ M5 V% ^, z2 z& N

    4 I+ V0 n0 x3 f* Z/ _4 u1 j& {& E    Once the base case is reached, the function starts returning values back up the call stack.
    6 t# y0 a. h, ~( ]4 [2 W" p1 T0 U: o: _( `% H* W5 H. ?) u1 G/ _' Q. Z
        These returned values are combined to produce the final result.& J6 j: U9 I0 v

    ; ~+ s. W" c: d6 G9 E; f0 o1 lFor factorial(5):
    $ h# W' {" Y$ @: x6 M3 d; {/ F
    4 r. O3 I' X: A6 A" L3 M  S
    4 c% J0 b2 O/ O0 cfactorial(5) = 5 * factorial(4)
    ! B  Q* C* B' W. F6 I8 n5 m& Sfactorial(4) = 4 * factorial(3)
    & _. T7 K+ Y8 s4 l1 h% j2 Wfactorial(3) = 3 * factorial(2)
    : B( C7 v+ Q! Q) Bfactorial(2) = 2 * factorial(1)
    ( \/ Q  p7 c8 D! ^+ O: Qfactorial(1) = 1 * factorial(0)& w: x. Z5 ^) Y) z
    factorial(0) = 1  # Base case' t1 d0 R5 y$ ]7 s' K  P
    ! T- }' P+ g% p0 H4 W
    Then, the results are combined:
    0 W* u7 ]7 w0 P1 G0 W* T. H; b8 C
    - y# e4 W. G* Y
    $ B  e; P: A8 g! Bfactorial(1) = 1 * 1 = 1
    9 ^; R! Q% S; W' ~factorial(2) = 2 * 1 = 2
    7 u' u1 [. a# f% ?  ?7 t6 pfactorial(3) = 3 * 2 = 6
    1 }  u" |' T. ^  q# o$ H2 \& Qfactorial(4) = 4 * 6 = 240 z3 _( D* S4 \3 M- C
    factorial(5) = 5 * 24 = 120; |1 E) o# L2 R1 g3 i' b  S7 E
    5 z" k2 M: C& J0 H$ J1 w% [
    Advantages of Recursion. s8 W$ ]% K1 e7 C( A3 ~

    : ]) I# l, e' K    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)./ K- u3 r2 I  R

    3 A+ }. X  ]: j, k    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    & s2 a5 {  J! }# X! N" E9 A
    * H4 z$ N* j, v  |& tDisadvantages of Recursion  B. H1 w9 D7 L2 l  o1 y

    ' Z9 z$ I" t# N' w2 L    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., _- E+ b- \% i
    % x3 S3 `2 L. ~. \) M( m
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).( x' a, r4 T( V/ f) g8 Z7 N: J

    . Z" t  y1 k7 m& w; [: wWhen to Use Recursion
    - E" i* S7 D9 }  G
    $ _8 ?2 l/ B% J+ V! y% C' b    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).5 B7 Z4 s4 h( Y" i
    6 y1 b+ l# l! ~' U$ v
        Problems with a clear base case and recursive case.. o4 @  p" C3 M8 s- M/ [

    ; F1 ]5 b/ Q' F  W+ VExample: Fibonacci Sequence# h4 a, `( c7 o0 L

    " V% }! X- L* eThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    / |% `0 k5 U' c- @8 e8 K7 c# K4 ~3 o
    ' \* m2 h1 f7 @" s. W  P    Base case: fib(0) = 0, fib(1) = 1- R6 W/ {3 @: {1 @0 f% d* G  |
    7 X0 ~- S3 {9 \, A+ D
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    # ?' X- E, h) T' Y
    / S% j' k) d7 g! H) q5 N5 Zpython
    2 |5 A+ v2 g# i6 |) N4 D) F7 |' P" t! K& m

    5 J5 k) a: Q; m9 L( ^def fibonacci(n):
    0 Z8 H. {7 |9 z9 M% o    # Base cases
    : c1 X. \+ Q9 D) P$ I8 V( |    if n == 0:
      a( Y/ S5 a3 Q$ Y        return 0
    : |& [/ Q8 y: W& a+ [3 \    elif n == 1:
    : w% I, T3 u1 E/ K5 F        return 17 V1 n2 @. U( b7 d9 K9 t
        # Recursive case
    / U  I% K4 J" k! E# i/ E7 n6 z    else:
    , P# C2 |: e( w6 r2 J/ }        return fibonacci(n - 1) + fibonacci(n - 2)( U" x- g6 C: i$ |9 U' {- Q4 f
    + M% \! v$ o" ^$ w3 h
    # Example usage
    3 B4 O7 T7 G6 Iprint(fibonacci(6))  # Output: 8
    & @/ p+ E8 {* C6 o# M! _) z. A5 ~( H2 ^: s; ?
    Tail Recursion
    2 }3 ]( e' E+ J+ J* N4 u5 h) q- [: F- v8 y
    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).
    / J+ i5 e1 ~/ d, T! e
    3 {5 Z. o, A- L! X) G3 YIn 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-16 11:01 , Processed in 0.033246 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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