设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 # I6 \+ }0 X; ?$ y3 t# _+ H) H

    5 U) u( `! Z# t& ^" b  \- v9 s解释的不错/ Y8 T8 W  h& w2 s" `$ [$ _
    , N2 J1 Y% i$ h- ]4 W- I9 Y1 m, _
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    : R' d. Z* t5 O8 l5 E1 q8 d
    . U, Y( o" B  {7 l" T! r  { 关键要素
    ( G8 T. y6 G0 [1 F: Y1. **基线条件(Base Case)**. N2 {7 z5 c- |
       - 递归终止的条件,防止无限循环$ H! G. }! j& Y/ `
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    * b: b7 \2 [! z& _% `9 v. t5 ]
    / d. @9 \4 O9 x0 s7 x2 ~2. **递归条件(Recursive Case)**
    - T3 h8 Y# y8 `8 q1 H& S9 q2 z( g8 Q   - 将原问题分解为更小的子问题
    + e. `, x# b( x  I4 H+ H( W   - 例如:n! = n × (n-1)!
    : [: @/ G  X4 |& M
    4 Y$ Z& G9 S* r5 s) q! o, [ 经典示例:计算阶乘5 Y$ v! L- `, J
    python
    ; R- ^) b$ ^9 |8 Z( z: f' H6 w; J0 _def factorial(n):
    " A8 v# P9 |' P$ O    if n == 0:        # 基线条件
    1 b9 M! i8 ]* `3 N1 @        return 1
    ; w8 t$ Y  [3 a9 c    else:             # 递归条件
    0 k3 _& n1 Z" m9 E        return n * factorial(n-1): K* k. _5 J. k) ~4 a6 M) }
    执行过程(以计算 3! 为例):
    8 s" g. F3 k! zfactorial(3)9 D7 `/ H9 q( T+ o1 R7 P
    3 * factorial(2)2 L+ ~! R( [& S
    3 * (2 * factorial(1))
    : P. X5 Q: J/ z0 q/ X3 * (2 * (1 * factorial(0)))
    % u& o5 V" ^# X, |4 y3 * (2 * (1 * 1)) = 6: `0 o+ L( X2 i* r! Y

    0 ]" [8 t( o1 M& W 递归思维要点
    ! Z4 i7 b  _$ c* `% E* {1 C3 c6 q1. **信任递归**:假设子问题已经解决,专注当前层逻辑. P) E3 E) }2 a0 V9 N' z# W0 [; x  g  D
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)7 L/ \/ U2 f2 Q& [" a% H; G) R
    3. **递推过程**:不断向下分解问题(递)
    0 C) `# @3 C0 S6 T- Q$ J4. **回溯过程**:组合子问题结果返回(归)- t9 E( m& x" a& B) U
    ! @* t9 }3 b1 K, u
    注意事项& P/ g$ M* C' g/ J
    必须要有终止条件. {, x2 a! S7 E7 o: N- d. T; T
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    & n3 U* |5 J* Z某些问题用递归更直观(如树遍历),但效率可能不如迭代
      \9 D( j! I3 H+ T" h0 S0 }尾递归优化可以提升效率(但Python不支持)
    . G4 _) @- N  E; N4 S, K5 J9 Q
    递归 vs 迭代
    7 D4 C# ~( G# j: }$ q|          | 递归                          | 迭代               |
    " W8 g; n0 o5 ?0 E: x2 L! E; o; Q|----------|-----------------------------|------------------|1 N; }1 }" }0 p4 i7 h( p, U9 x
    | 实现方式    | 函数自调用                        | 循环结构            |8 N& j4 u+ w4 ]( d* k  H! y
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |! p6 ?2 _2 W6 T7 Q7 Y
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |& i+ o0 y) X5 R; _
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |% Q  y3 |1 S- T0 [( X$ q: ]

    . ^- G; d# Z/ t2 i 经典递归应用场景
    2 r7 A5 h( i# \' }1. 文件系统遍历(目录树结构)
    8 x7 v6 Q1 j0 L0 P- ?2. 快速排序/归并排序算法
    3 L! f: w' D# T; }5 v5 y! v3. 汉诺塔问题
    - W+ M2 }! ^  B- D4. 二叉树遍历(前序/中序/后序): E+ l3 w% ~4 V: W
    5. 生成所有可能的组合(回溯算法)" u; |) l: C0 W
    : G! r3 c$ Z2 U" ^$ r
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    无聊
    昨天 11:23
  • 签到天数: 3108 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    6 l  e; V' N) K我推理机的核心算法应该是二叉树遍历的变种。0 _' v0 r" G) R( y  v4 I9 T
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:; F! l5 R3 j4 D: G; I
    Key Idea of Recursion
    , L% R' p" `" r$ t, E* E" A* r, d2 r' L- x* V' R
    A recursive function solves a problem by:( {' ?! \9 v- K$ C6 s9 K
    * R# t! `: Q/ `9 @; L# i  Q' u4 \3 `
        Breaking the problem into smaller instances of the same problem.
    : T8 \8 L# m. q2 H; L
    9 [' ^! S$ n: V* y! ]) Y  {    Solving the smallest instance directly (base case).3 ~* `4 W! N1 f0 H# L# Q

    % c2 d; `7 Q3 n  q    Combining the results of smaller instances to solve the larger problem.6 l# z6 n6 p9 H
    ( Q5 b1 e+ c+ T3 c% \+ u
    Components of a Recursive Function, D3 f( c( i: K" a: \

    $ V; K6 v0 p# a% G; T) B* p! z    Base Case:
    6 _* R( B; J3 A+ R  d- Q& p2 k3 T5 t# ?' R4 q" I$ M, a
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.4 l  s! l" ?2 ]3 X. e
    " a+ ^9 V9 M- D# x- L  _. y& Y  V
            It acts as the stopping condition to prevent infinite recursion.
    4 v, z& o- a+ f# P2 Y" ^
    - e$ W" `7 r0 g( i        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    # K/ V5 }+ ]( l6 y) M% N: ~9 t
    : e. H$ B! i0 q$ E- j    Recursive Case:$ e; @: e' y% B4 Z5 X# P

    " L3 k& @* w+ B; v7 w: L/ y. t        This is where the function calls itself with a smaller or simpler version of the problem.
    ! a4 |' d( {) m4 O  O9 w. @: [5 ?2 E/ P
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1)., V& l  g  U3 G/ [

      f) w6 K- ~7 P* hExample: Factorial Calculation
    ' T! K$ s8 x1 z3 L% m% v+ {# O, B& H# a
    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:
    ) g5 [# R5 o) f" a9 b( |+ k' P# K( h  }/ g% x; o' k
        Base case: 0! = 1
    ! [. W! i) t. e9 q5 r1 ^+ R1 ^# B0 b! `0 S0 q4 a9 t) j
        Recursive case: n! = n * (n-1)!! S3 g, M- y5 `$ `7 `6 m

      t# K0 Q3 m% c4 q: |0 e3 xHere’s how it looks in code (Python):7 ?9 C/ i, Z. f0 J
    python
    ' n" E3 p1 f/ a; }+ C5 q* L4 M7 G  c7 p. T/ A! L+ l

    ! F/ M" N; t8 g2 ~: S5 [def factorial(n):
    + {0 K$ G* [* u6 H4 g9 K- D- p    # Base case
    * V! Q. x. ^* E5 |( p- x    if n == 0:4 {2 ?. t2 u2 L6 R& o
            return 14 k" {! S, f+ @4 m7 E
        # Recursive case
    ! a+ L! U  a5 q6 |  ~) h$ m* L7 S* |    else:& R( I( N6 \: K& O
            return n * factorial(n - 1)
    ! ]+ I+ ]/ \" L, Q* n' r4 \& v9 m) A3 u- F  U
    # Example usage$ k. ?- K- n& H( G. \
    print(factorial(5))  # Output: 120' `( h1 P7 ^( Y5 Q( u
    2 r! [% A5 r& X; ^. m6 ^, }
    How Recursion Works
    0 |  [2 i1 X4 N3 g# [5 R. b( x/ G& c! p2 F/ D) ~
        The function keeps calling itself with smaller inputs until it reaches the base case.: O- t3 _5 ^  _5 Z. O) c1 N' u$ q

    . x( U& w' ~% x# |: p    Once the base case is reached, the function starts returning values back up the call stack.
    # a) K) M( _+ T# v0 T$ ^
    : e. G; k2 X! ~. A$ l0 z    These returned values are combined to produce the final result.2 p  }8 j2 S* N- K7 i- I
      T" t" [8 _5 Y
    For factorial(5):! M; C2 {( p/ y7 q9 Y! }* i) e: O

    5 C3 M# D/ p$ Q
    2 Q: ?1 |. p) i! y! tfactorial(5) = 5 * factorial(4)
    4 n. q1 b- j+ Y' Z: H4 nfactorial(4) = 4 * factorial(3)  [5 e6 i6 W! y
    factorial(3) = 3 * factorial(2): i9 q! O' X) ?  a( C' `  a
    factorial(2) = 2 * factorial(1)
    3 }% f% F5 U+ Q1 I: sfactorial(1) = 1 * factorial(0)# a$ L. K  b  s: d2 Q2 ]4 K6 D, e6 k4 t
    factorial(0) = 1  # Base case. t( O7 }$ M$ m4 q' P. U# f2 I

    % _& F6 }5 f- V' ^. jThen, the results are combined:/ [9 H) U# j5 Q( d# q$ U2 u# f1 `/ o
    - J( u5 S4 h# F  e1 y5 ?
    9 \/ h# B# {4 J
    factorial(1) = 1 * 1 = 1& a- I: A. r; K( x$ `
    factorial(2) = 2 * 1 = 2
    / T: _1 u2 S% {! [factorial(3) = 3 * 2 = 6% U8 d2 c; B& \& L! V& N2 Z) n4 d
    factorial(4) = 4 * 6 = 242 F. Y0 ^) l' Z
    factorial(5) = 5 * 24 = 1204 K" q8 A7 _6 f& `- }" b

    2 s8 p- v2 a  n' ]0 k" x* ]' z$ j7 ?Advantages of Recursion. q3 x# ^8 m# ~

    - [5 N3 I  G4 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).! `* q; C( f- f3 w; T( L# C, r

    ! |+ ~% e- J$ }# [1 R7 a    Readability: Recursive code can be more readable and concise compared to iterative solutions.+ g6 s8 o3 F  P# W+ C8 A& T5 m

    - {  W9 k* t) y! t& JDisadvantages of Recursion
    - h$ |9 R1 y; d0 Z" S& p! ~( Q
    7 j0 k' t: q4 r: o' P+ J; 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.
    # e, p, e( i3 v3 a8 j/ z. [0 X( N0 u+ S
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    : y2 Q& o& v4 k& L+ i& o+ v& i8 n
    6 y4 h/ o2 {9 X( W. p# Z! _3 ZWhen to Use Recursion( ]/ F9 P! G0 i% b' s) K
    % m( ~' G7 w$ }# a9 A+ N$ C
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).$ R3 B8 I# ~$ K# d) x- `& z

    ' G2 H* n6 |& i    Problems with a clear base case and recursive case.
    - B2 z% ?$ Y( D1 t" |0 U$ k' F
    ; T' b; a" J" D8 V* ^- gExample: Fibonacci Sequence. ]! G2 T* L" i3 W# q2 m# J4 ^5 ^

    " H- `$ ^4 Y4 Q7 w* a& rThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    % R* J2 h% n% E
    2 b2 q0 i6 p# @* J( A    Base case: fib(0) = 0, fib(1) = 1/ @' X! Q0 L6 k: Q, e4 J
    " j% {6 @- m( Y
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    & ~4 H+ y) c) x- \  b. Y& p+ ?4 K( Z% ~" }& u: M5 q# ~7 N, s
    python8 Z* K( {' {) g/ ?2 e
    ) x1 |, n- o! ~
    * A, F& k: h3 m# J8 n' ?
    def fibonacci(n):8 ]. D) S; O4 e6 n$ w! L
        # Base cases
    1 z; |5 b  o: c. B+ G    if n == 0:! G2 u2 r9 C8 F0 |# {" q5 \" M3 [9 d
            return 08 K7 q" Q; K: e! M0 V7 h0 ]- U
        elif n == 1:7 {6 I9 F3 K. }: P
            return 1
    7 y# @" y3 p$ u+ ]* w9 N: t    # Recursive case
    # C* }/ h5 t+ ?    else:4 M0 R0 n2 H* s7 l+ P/ {' j$ ~
            return fibonacci(n - 1) + fibonacci(n - 2)
    * P5 w: H- A" \% L3 ]
    ' t% N3 s6 L% o0 m# |! }8 L8 l# Example usage
    4 i7 l0 O+ r( ~8 Uprint(fibonacci(6))  # Output: 8
    " o( Z! r2 ^0 Y, L. a  R0 P" B6 N; g- _6 S$ o9 B: {. ^& u
    Tail Recursion9 l8 t* i3 s. h
    % ], {, x6 u( `, E5 R
    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).: \( G- ?* V" y$ [
    ! {( C0 c- G1 h2 z# i& a3 k
    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, 2025-12-5 07:05 , Processed in 0.033189 second(s), 20 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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