设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    % |. z3 [( h0 f0 E) W, L
      b* r' E' |. U- k解释的不错
    / z8 l( x+ ]- Z5 O6 G' o: R$ G$ y' z7 u3 \+ J: v, E: v3 m6 t9 h
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    / Q& a0 E9 }" n! d: H4 [
    & ~$ S3 n0 X. U, G0 T. x 关键要素
    # ]: s8 O+ o) p5 ^5 U1. **基线条件(Base Case)**- h4 r9 I2 \! j; v2 ]9 _7 o
       - 递归终止的条件,防止无限循环* l# t: \7 i1 c; j8 t3 s
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1- R! K! D; b6 Z" `5 m% m% _4 ?

    : L( m* z! ?3 B$ J( a- E4 a2. **递归条件(Recursive Case)**
    * E- ^3 E4 S9 B0 I% Y4 }( s* f0 _# R' b   - 将原问题分解为更小的子问题
    & Z- h. t9 P. p8 w) Z   - 例如:n! = n × (n-1)!! p+ ^: Q$ G6 D1 p( k6 J
      U6 r2 d# `1 t- ?0 e
    经典示例:计算阶乘
    8 Q+ Y' Q8 }6 hpython
    2 Z) T" K! \& _: pdef factorial(n):
    % I  d$ a- l' K) t3 J- |3 r    if n == 0:        # 基线条件/ a: a& L; i" G  `
            return 1
    , f4 W7 D% J2 k; l) j7 V! W    else:             # 递归条件
    ! u) T0 k- @; t        return n * factorial(n-1); r' k( J; P1 h+ ~+ x
    执行过程(以计算 3! 为例):) W9 `: ?+ K7 b' {" p; R3 t
    factorial(3)5 X* B( Q, Q& W3 a* q4 S6 ^- q: V
    3 * factorial(2)
    + X, a9 ^# k. o& f3 * (2 * factorial(1))
    3 y, j& U& |, B4 k1 {, R/ d3 * (2 * (1 * factorial(0)))
    % h& G2 Q5 l" e( y3 * (2 * (1 * 1)) = 68 G% ~' c; i( M% j7 f: a+ e
    3 c5 z9 e  C  {6 O; ?
    递归思维要点$ z. c4 S! e& L; Y2 Z) X& _
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑( `+ O+ x& {% b6 {9 z  r
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    0 g5 \" T' P' j3. **递推过程**:不断向下分解问题(递)
    / o4 A- p1 O2 B6 d  v4. **回溯过程**:组合子问题结果返回(归)
    / Y! b$ w' O0 x6 e5 }, q
    ! A0 s2 U/ z# K+ H4 r" q* ^3 g 注意事项
    7 Y. L/ f1 |' C. \5 Y  P5 F必须要有终止条件
    * E) E8 h( I" Q+ e$ }, h递归深度过大可能导致栈溢出(Python默认递归深度约1000层)1 ~, B8 O& ?; `4 y8 D+ _
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    * b( v8 o! T3 f- Z' g尾递归优化可以提升效率(但Python不支持)/ v+ t- ~# i5 F# b& `* t

    7 V5 a( l( K$ e 递归 vs 迭代
    + S( u% r+ [6 p( R* W8 C1 M|          | 递归                          | 迭代               |
    / ?' l4 ^- v$ u3 N|----------|-----------------------------|------------------|; D4 A! T4 Z* q4 L' z
    | 实现方式    | 函数自调用                        | 循环结构            |
    ( V, N; F: }8 B- [2 f# r| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |% ^9 w! @+ G/ ]0 d5 F4 S
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    4 L$ j5 s( L) b  C| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |! K, E) \0 _$ \$ C* x4 ?
    , M: @  M8 O) o9 v0 j1 w
    经典递归应用场景
    # \% v! _- Y, P1. 文件系统遍历(目录树结构)' u% p, C  P- p5 l
    2. 快速排序/归并排序算法' ~  Y' B5 _4 i
    3. 汉诺塔问题
    : P5 U) L; ]. M) a4. 二叉树遍历(前序/中序/后序)
    ; S% v& L# x# C. ~- X2 Z- C7 v0 `" o! x5. 生成所有可能的组合(回溯算法)
      o8 R$ s3 j( ~0 L/ j0 ^
    & u6 V; O- P0 C; P试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    无聊
    14 小时前
  • 签到天数: 3236 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    # e# L  f, T2 C  d8 p7 q2 w我推理机的核心算法应该是二叉树遍历的变种。6 [' `9 U: C1 P: P& Y# |
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    - P. p0 q/ o8 g2 a, FKey Idea of Recursion( l2 b( h3 J! V. k8 Y4 |3 |9 f: I7 a2 D

    ; h! K3 J6 c% R/ w  j# W/ i# ?8 gA recursive function solves a problem by:4 z" [, K& H  w  k+ `' l
    ' b  U9 a) O! l( L3 P
        Breaking the problem into smaller instances of the same problem.
    7 \, L% S2 i  D) v, _: C+ n. e; n, N8 H# l( d8 @5 [  e
        Solving the smallest instance directly (base case).9 G% d; F9 w/ p0 D/ v0 Z( W* O
    ; U  M; `8 z( c( o. m
        Combining the results of smaller instances to solve the larger problem., n* O) y: p) i: `- O+ v

    8 |2 z2 H. C( f" IComponents of a Recursive Function
      b* q/ O: Q4 K) V4 B7 v' @/ e+ F
    # r) U3 B: u% H3 m4 b. [    Base Case:
    - I5 }- j; v& }: [
    . I, U' A# I  ?" c/ ?! L  w        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.4 Y$ b8 g: o0 ?2 `4 g3 q: i" U

    * W& y- `1 h5 t; G2 n        It acts as the stopping condition to prevent infinite recursion.
    ) v# F1 q0 f- m* C  g+ h/ _
    + }! x" u- s/ k& w, `        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.8 Y! ]5 h: a& U2 [

    & ?7 i% n1 K6 G& q    Recursive Case:& W1 N6 f5 u  m  R

    4 I/ K+ f5 {: \! f        This is where the function calls itself with a smaller or simpler version of the problem.
    ( w) z' o& C* y/ W3 C4 F) ~* H, u  }8 H: N
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    ) [- D" A0 ^7 L7 E) V2 B! l+ I* \3 O* \0 c: T4 M3 C
    Example: Factorial Calculation3 I) L& P. {: ]3 ^5 @  q

    + j& S; l. f# c, \+ z$ xThe 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:
    5 v3 t) L2 \) Q$ P) j7 N" G; p4 [6 q
        Base case: 0! = 16 I2 M6 M, r' Z+ _" f
    , T+ v. r5 _- f; M
        Recursive case: n! = n * (n-1)!1 o, ]2 j) ^; b
    ' ]3 _* Z# Q& }
    Here’s how it looks in code (Python):, s- a' \* p# J) L1 e
    python& E. \) |0 _( i+ m) F) J
    ' q* X8 @* b1 G0 q1 J
    % {6 f) o$ J2 B9 o3 G& B
    def factorial(n):
    + m$ x/ r* z  u+ A    # Base case
    6 X4 r6 J3 m5 |9 M    if n == 0:
    9 L! D* N& w( I+ L        return 1
    " _% _. ?) x! ]5 V    # Recursive case
    9 ?+ A+ U7 u0 R! `    else:2 S  D6 r. L) c! g  k
            return n * factorial(n - 1)$ P4 n8 W' B0 e" K
    # K, w- O4 s1 d; J# Z
    # Example usage
    2 F* D+ N, k; e2 [) gprint(factorial(5))  # Output: 1207 [( {# i% m  U3 m

    5 a. A: n9 A4 ~  bHow Recursion Works
    , E% p' \) ]# m1 U5 o
    $ t5 k. S. W# f    The function keeps calling itself with smaller inputs until it reaches the base case.4 I/ _! T7 n* |5 u; c4 w" F
    - D* r; D, Z5 N, J
        Once the base case is reached, the function starts returning values back up the call stack.: m5 y: h" T0 _# v/ X4 F
    ( X& W9 G, L$ W. l
        These returned values are combined to produce the final result.
    & R% N# c1 t% t" w" B2 r' K! E2 ~9 e# a+ q1 j- e8 R
    For factorial(5):
    * X" V3 V, }/ E2 \% k4 k; K2 `! s1 c+ @
    ' a: z& i% |; _* I& a# t
    factorial(5) = 5 * factorial(4)) ?) {7 v' {; L% D7 i# C
    factorial(4) = 4 * factorial(3)( h! p: U4 l" ~& f
    factorial(3) = 3 * factorial(2)
    % l. m/ N, b$ Q+ T. G$ l) C7 Jfactorial(2) = 2 * factorial(1)
    + H. V( x% R# |0 {factorial(1) = 1 * factorial(0)
    ! n, m7 K8 |" D; j! O1 D9 M4 @/ Zfactorial(0) = 1  # Base case8 e  V, F" X  R5 Q' n- J. i2 l

    6 Y* S* y% \/ l& f! h5 m# |Then, the results are combined:/ k! g2 l7 E* u  X/ B7 B; O
    8 h5 i. O$ e& B

    $ N$ I$ D  ?! c, O7 T/ B- T" Wfactorial(1) = 1 * 1 = 1
    ' f2 d( y0 n$ E9 }2 Z# ifactorial(2) = 2 * 1 = 2
    ) m+ _: c2 e) {4 S. L3 ?factorial(3) = 3 * 2 = 6" W% o( ^2 w( N7 H* p- R  ^1 m
    factorial(4) = 4 * 6 = 24/ e/ a: {- L4 F1 S. Z9 ^
    factorial(5) = 5 * 24 = 120
    $ i5 M, j4 T; k. ^2 m3 a+ W* b& X6 K# [- B; c- {2 ?
    Advantages of Recursion' a: V+ D" U- `
    ( T! g: X. z- C( B! k3 [
        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).% |1 I# K3 N0 G  Q! ?8 e
    . s/ D4 e$ k& b5 q# Q0 i
        Readability: Recursive code can be more readable and concise compared to iterative solutions.7 j7 X6 g% E* o. p; h  S7 M
    5 v* L" z  Z, j, Q; U/ u# e& X# r
    Disadvantages of Recursion' }8 s4 O0 L" Y) L! J0 Q4 u3 p- W
    , w' b9 [6 M9 h, |) v# {  V
        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.
    ( i  b  u& B8 F# r0 }# V' b3 P3 U5 I
    0 |# f9 I! @, d+ M  _    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).$ q9 _) V5 Z6 T% ^1 \8 o/ O/ }9 J3 e/ P

    5 p9 I: B) H; _: |When to Use Recursion
    : ~7 O5 x, c' Y
    7 q' L. R( W- L" g5 L    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    % c- F* z( `: K- U+ Y% p5 g9 U! }2 R" B- p
        Problems with a clear base case and recursive case.
    8 e4 |  a8 k! u# R  \1 H4 {
    " k2 a" _7 l2 e3 t) xExample: Fibonacci Sequence
      Z" _4 o2 y/ H5 c4 P% V6 R: Q/ L) m, W8 u% {4 O' P" p! N6 r, u
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:/ G: ^3 I; W% k. e

      v1 W+ u. f$ R6 w    Base case: fib(0) = 0, fib(1) = 1
    " f! v1 g% W  v$ Y4 d
    : I) r6 d# p& T    Recursive case: fib(n) = fib(n-1) + fib(n-2)+ e1 \* b* P, A3 Q
    % K: s# f/ X0 h; U. W
    python/ y5 N( G" c. j; C1 c% r

    ' E: }3 Y) U7 h. a& g5 L- z
    - _( U* ^% Y' }, K. f& rdef fibonacci(n):9 p1 N0 w, f1 P7 ~
        # Base cases
    : B7 k: M7 m% T3 P! H; R    if n == 0:* k4 H* o; [/ `5 l7 R9 y  z$ U
            return 07 M" O  Z) H. S9 ]
        elif n == 1:- O0 D9 d5 B; B  |/ D/ n4 P5 Z
            return 17 v/ M6 A9 n& T" r' K& A
        # Recursive case- p1 x  [* _8 k2 G' c: i
        else:; m9 s1 C% ]# ?' B( w/ x
            return fibonacci(n - 1) + fibonacci(n - 2)
    + \7 \1 T6 O0 s+ j) G- y9 n  v, v. O' J1 T
    # Example usage$ u! M' z5 K, `0 D
    print(fibonacci(6))  # Output: 8! g+ `1 k' A6 v9 b4 x; c

    + ]- V- \  W( S+ qTail Recursion
    5 [4 r, h$ z+ e0 _$ _: J
    : L) U: ^& ^: U- c, ^4 ^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  A" i+ |% F: \
    1 `$ X- U! _4 ~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-5-10 22:03 , Processed in 0.064397 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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