设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    4 Q0 [0 |6 p% _, g+ p2 |' P
    $ H+ A* V3 @1 t8 N4 a2 x7 u解释的不错
    # K7 s0 q' _5 ~& P2 k' `8 }5 @3 G" u1 f7 z0 l. G6 a0 T
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。$ ]' Y3 G, p% I
      f# g0 i9 I' X
    关键要素
    / T( Z% }+ U& B7 J8 _1. **基线条件(Base Case)**$ x/ g9 Y/ X2 G+ I9 b6 S# H
       - 递归终止的条件,防止无限循环7 r- u8 D% h( q' ^2 j. O
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1/ d  W9 J1 e2 v1 z/ [2 D
    , C; r, X9 ~1 J7 p
    2. **递归条件(Recursive Case)**3 b( b. {, z9 f; i- K; T
       - 将原问题分解为更小的子问题, i  Z) F% s9 K2 x. O: A' `  v* S
       - 例如:n! = n × (n-1)!0 h6 r  [$ w* i4 i
    # @7 \* ^- ?) k2 J% K0 E
    经典示例:计算阶乘
    " f/ O1 k! z- m3 d5 _' ppython
    . ?( g: ?" n9 m0 l6 h  odef factorial(n):
    ) d5 ~- M. j$ G+ G: M9 ^5 B+ o    if n == 0:        # 基线条件, c# M$ X! R. p, ^7 ^; g) D
            return 1( T) `0 j* _  j1 Y" N
        else:             # 递归条件4 `, x$ A2 m* P* y
            return n * factorial(n-1)
    ) R% ^3 t; `4 @" I2 w' I执行过程(以计算 3! 为例):+ F% Y  Q4 m* ~. @& k( g  |9 C6 [
    factorial(3)+ k' Q3 C4 t' O$ c" _& m
    3 * factorial(2)
    % J3 T8 s( D/ W0 t1 \6 C3 * (2 * factorial(1))! J1 a! s3 J5 b
    3 * (2 * (1 * factorial(0)))
    ' H% r+ J% T1 J2 f) R' R3 * (2 * (1 * 1)) = 6; W0 U1 R7 i7 @- l' e: l

    & p- R6 D* W. ?8 m% r4 M3 Z 递归思维要点
    8 X8 |4 A' i4 j& V& }1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    3 b. _& |- B) R# ]2. **栈结构**:每次调用都会创建新的栈帧(内存空间)" F/ E1 Z7 c5 t' S! x
    3. **递推过程**:不断向下分解问题(递)
    9 X; B8 ?7 p+ V5 y. }' D4. **回溯过程**:组合子问题结果返回(归)+ \( q- _( Y3 w& ]+ p

    ' f% D7 J# t& Y8 V, d 注意事项4 a. P1 K+ U/ f3 ?% N
    必须要有终止条件
    4 `" M1 k+ g7 m: m4 P$ D# w递归深度过大可能导致栈溢出(Python默认递归深度约1000层)" L" @; D$ j/ A
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    * Y1 D+ }' @7 g1 S/ j尾递归优化可以提升效率(但Python不支持)4 p) q& _. C+ u2 R7 c
    5 V% X; L& m7 ^4 ?
    递归 vs 迭代% Z* ]0 k' g3 `! `
    |          | 递归                          | 迭代               |7 j6 m% A! A& ~
    |----------|-----------------------------|------------------|% k+ n" t& H  o" _5 G
    | 实现方式    | 函数自调用                        | 循环结构            |
    9 h+ b! V( g) v$ q$ a| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    / S" G! l0 h" E9 b' E$ }0 ^/ A| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    + @3 w  Z$ m& L1 O! H- Z& t| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    # I" i& h! J& q& V  X$ N6 p/ {+ \) J4 ?  m
    经典递归应用场景
    * E8 y  F4 i8 Q' s1. 文件系统遍历(目录树结构)- n: ^5 @6 O. a$ {
    2. 快速排序/归并排序算法1 A% l' r$ \, B
    3. 汉诺塔问题
    8 \6 m  i3 G8 @! [4 d4. 二叉树遍历(前序/中序/后序)
    4 d. s3 l) g/ n5. 生成所有可能的组合(回溯算法)' ^3 x2 [) ^. ?% J" T

      c0 K* Q6 I4 D0 l3 t3 L试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    难过
    12 小时前
  • 签到天数: 3303 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,5 g% ]" Q6 T) j. i2 X
    我推理机的核心算法应该是二叉树遍历的变种。! R7 x8 O! r% c) {$ V% y3 N
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    3 I: N+ {) g. ~- f; `" h: G* ^Key Idea of Recursion+ E3 t. X- m" ^# J0 _" F
    7 L" I% K/ a. j2 W* C% k% j0 M
    A recursive function solves a problem by:2 \7 v' ^6 R) M2 G/ a+ p
    " N6 r) N& }0 @6 |7 b! v% N
        Breaking the problem into smaller instances of the same problem.2 c$ t9 ^. a( h
    2 N4 a( P! B3 X$ }- g
        Solving the smallest instance directly (base case).- r5 `- W5 A4 V& b* S- s0 M/ }. t# _
    0 S) ?) K+ E) s2 b' l, A' x; D
        Combining the results of smaller instances to solve the larger problem.
    0 z/ X9 j+ ~7 o; e- k+ u  z6 S% n
    & c4 @2 O' n5 u$ |5 v& ~* |Components of a Recursive Function
      M1 v- a0 w+ K/ b: ?) `' A3 V7 r% r- M4 a( G- f3 A) j# Y
        Base Case:
    0 o1 ]- O1 D1 Q, q: ^! `' O# E; W
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    9 {/ m3 g7 V- ?  L4 j' _  U+ D, _. E9 L9 y) J0 y: [$ Z
            It acts as the stopping condition to prevent infinite recursion.
    4 j& {7 A  q' ?8 e$ |' b0 [
    2 K5 C9 y# \6 z& V' s; P# j        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    + @: A2 _0 Z- I; q: Q0 P: u7 _; o/ D- p6 g1 b- S* B; S) v
        Recursive Case:" W$ D0 m2 g9 P9 N! u) X
    , G1 c) A9 c. u, k6 ]  l2 v
            This is where the function calls itself with a smaller or simpler version of the problem., E8 A5 i8 I& t! t5 g
    9 T3 m" H5 Z' {! h
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    ' W  e9 P; i  B+ s/ Y& I6 h1 Z
    " o7 R3 W3 V3 n! |! LExample: Factorial Calculation
    ; A+ y2 `. O& i
    4 z% b2 E/ x) pThe 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:1 g3 M  ~! H. `" m
    3 L( p# C! [' W3 Z0 m
        Base case: 0! = 1
    8 }( R* g) e1 m4 C" x2 @2 l0 _; }1 B: |  f9 m
        Recursive case: n! = n * (n-1)!5 D( @# H- g' f8 k) n

    1 V- P8 }& t( S, R% q4 Z& jHere’s how it looks in code (Python):9 t7 A6 r8 ~" A7 U9 ^
    python* T7 A7 f$ W- h; R7 l

    5 R+ r& {: H- |$ t1 X
    ' ?6 k1 d  n% B6 \7 ^3 cdef factorial(n):. ^8 e5 X8 a0 p+ T' f# }
        # Base case+ @+ d6 z$ Q+ h$ \3 F4 w2 C- x
        if n == 0:' E* X6 N  F' r* Z5 F- ?' ^
            return 1
    4 [& |2 M9 `' w: l7 e: ~, N' u    # Recursive case; S, f4 M* d+ X% ^; }% U
        else:$ B  l# B/ h4 I8 ~7 E7 c6 u2 `/ M
            return n * factorial(n - 1)! B" G" Q+ F% ?1 E, S, P8 U
    0 b9 C/ z; J' _" p: f1 N: J
    # Example usage8 T; X- E2 c) l5 k5 K/ b5 n2 W
    print(factorial(5))  # Output: 120) l6 J  z2 T8 x+ \

    - |, u2 l, f) @& X  {How Recursion Works
    " o- B# H$ l, P: T) s# r4 Y3 g
    : \/ ?: @* L! U( s$ I( `* \    The function keeps calling itself with smaller inputs until it reaches the base case.
    ! y4 s0 N  w) ]$ x9 \5 b9 m
      E4 V$ n2 L: t# W    Once the base case is reached, the function starts returning values back up the call stack.. G7 w# b" P" }( D$ m2 T% g; L

    : w" v4 V0 i# b# B8 t' U    These returned values are combined to produce the final result.
    ; O' S( M' o4 q2 ^2 J( U4 S4 S1 t! V. J/ T$ w8 d7 A) T- {
    For factorial(5):7 `8 _8 Y- n7 M! I
    9 |) G# G  v4 Q8 O$ U; ^9 S

    6 G5 @0 e1 `$ @. }factorial(5) = 5 * factorial(4)
    , k, E; _( Q0 k$ W. e* ~, wfactorial(4) = 4 * factorial(3)
    $ ?1 O- ^# E: sfactorial(3) = 3 * factorial(2)5 B+ i# \4 I4 a* J0 I1 Q5 C
    factorial(2) = 2 * factorial(1)
    . P7 T' y/ j( S1 I6 Ifactorial(1) = 1 * factorial(0)3 w! \( J; D, p. {0 h
    factorial(0) = 1  # Base case
    ' p0 d4 ]8 Q- K
    ( f/ T% c: k1 E2 ~Then, the results are combined:
    / Q. j) {6 U  ^# s6 J  O( i% H: b/ K, M% N( f
    5 a( D3 Q1 d. C) ?/ F
    factorial(1) = 1 * 1 = 10 t) q4 z4 b" X0 e& P
    factorial(2) = 2 * 1 = 2
    / p% {# ]  K8 ^0 u( Qfactorial(3) = 3 * 2 = 6
    4 \# |9 Q8 L. J( Z; n8 S9 E/ kfactorial(4) = 4 * 6 = 24
    ' s" p) ]) d5 I# G/ M4 [factorial(5) = 5 * 24 = 120
    % V. I3 G* Z( b# T; m4 S5 `; f; k" K" Y' N
    Advantages of Recursion" k6 w4 D# B( Y+ Z2 G) y

    / c+ ^5 Z" I  f  E+ f( N$ U  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).' V# @4 V& S' W

    + o& T) J" r0 _: u    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    ! C- B" z& q) v$ }+ j0 f" ~( J. e2 U$ c% I5 U5 P: o0 `
    Disadvantages of Recursion
    & z; `; j' w0 _/ N+ ?+ ^
    ! k/ C6 o% V. \9 h7 l; G0 B: n    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 y5 f  G: d2 Z: K% c% Z9 h1 j# g
    ) s! ]" G$ y6 T. y6 j3 B$ b& j
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).! k% |1 ~$ }6 O, J$ |# `

    : T. ?0 T. X0 S2 T( LWhen to Use Recursion
    " |7 I# ~; G2 s
    5 C  j" p6 I2 S1 K& t) J1 Z    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).+ p, ~* M4 w% Q$ E, H6 G+ p! o

    5 c0 Z4 U; F* J. @$ E( R+ q    Problems with a clear base case and recursive case.
    % h5 P/ Y7 l4 j! q5 p
    & U( U* M  G) a9 ?' tExample: Fibonacci Sequence
    $ A3 t" u1 J, y/ W" @
    3 M' n1 ^1 {5 I9 S6 VThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    & S! T7 D  O, V( h0 E) {5 K7 h9 r
    8 ^3 z" E5 T! S" Q; n! ^' W    Base case: fib(0) = 0, fib(1) = 13 M5 A* V5 d, f0 @5 C1 [8 ?
    3 l; }2 `* E2 W1 |# z
        Recursive case: fib(n) = fib(n-1) + fib(n-2)/ Q7 A. e' [3 i& f

    4 g  [2 m/ O0 I6 {& C( Vpython$ y) G+ Z/ \$ I2 g/ F+ ]

    8 P# w9 g$ J5 K3 L. M" I+ R/ ?8 Z& o8 d
    # b# ^: ~5 X" D) E- Pdef fibonacci(n):5 f0 z( d4 D8 v+ |7 k
        # Base cases
    4 Y9 K# f" t9 n. I# {- ?$ [    if n == 0:+ B; B6 z0 A* c
            return 0
    0 b3 g2 g( D% J6 d% ~6 G3 y    elif n == 1:
    4 B2 `# _3 Z# V: l/ g2 _9 r* K; `        return 1( _+ N  O1 G4 x  W
        # Recursive case- H$ z" u0 T! E; Q
        else:
    1 J) q) \" @  p' P$ K        return fibonacci(n - 1) + fibonacci(n - 2)- O  n! e+ s1 b$ V5 k

    3 h* v; u4 g  D8 @# Example usage
    0 w9 c6 ]7 ]2 s1 h$ I" D; cprint(fibonacci(6))  # Output: 8$ T0 w8 O, q1 s! D
    7 k; D# e# ?8 r& V* V$ i
    Tail Recursion$ L6 q+ m3 ?5 m7 S
    1 h- p' Q! m- v" P1 w
    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* T$ h0 B. i1 p
    9 c- l. Z  H9 f8 ~& E/ }6 F
    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-7-22 19:08 , Processed in 0.057817 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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