设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    ) C5 A% Y# V7 d4 G
    ! \+ \/ s8 @1 l. K" B解释的不错
    / y! u$ V: M) v& f  B. e) \/ u' \3 K+ M) ]8 \
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。" u/ L4 C6 B3 S( A0 K+ m' B" K

    ; A) C. \3 G  Q 关键要素
    $ w7 R, R+ _% s- H1. **基线条件(Base Case)**
    1 W1 t* p3 I# Q/ ]  Q- Y   - 递归终止的条件,防止无限循环+ @* |5 I9 \( ^+ l
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1' ~$ Z2 z1 F0 L; F: P5 h6 V
    7 J, j% }2 w1 n/ B
    2. **递归条件(Recursive Case)**, p9 n3 c: R; E0 ^  e8 S
       - 将原问题分解为更小的子问题
    & L1 W1 X- l1 s   - 例如:n! = n × (n-1)!7 B8 Y( H  w$ t- o+ N# s

    8 b2 O0 Y' K, T0 p. W. d 经典示例:计算阶乘. R- d; Y9 b8 Y  G& q& a
    python
    / t* m$ B( d- B7 Pdef factorial(n):
    7 l2 v9 i" _8 b4 _$ E: y    if n == 0:        # 基线条件# l3 a1 t( k; O1 g
            return 1+ a8 c+ K: B. m7 q% W
        else:             # 递归条件( |- f5 f, c: }2 l" N; U- p
            return n * factorial(n-1); B+ x, p( [: c0 s0 a6 J
    执行过程(以计算 3! 为例):2 m: S9 _4 v# s# N
    factorial(3)
    2 h1 J$ U9 @& D, e+ l& @# N' Z3 * factorial(2). ?7 i3 }) K$ q0 |9 D" E$ l
    3 * (2 * factorial(1))2 C! L4 ~8 _/ f0 b: g) a* ?
    3 * (2 * (1 * factorial(0)))$ A  P5 t. }% E" q- b
    3 * (2 * (1 * 1)) = 6& C' Y8 @# g& d- H3 N1 y
    ( w5 l/ X! y+ m3 o" u+ J
    递归思维要点- B. t! d* V% z1 |# k! `
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑4 D3 G1 J/ P& |8 A
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)7 Z( f5 N4 |6 n! k, m; f2 w3 O0 e* ~
    3. **递推过程**:不断向下分解问题(递)
    9 Y( B) T9 P5 h0 ?, I4. **回溯过程**:组合子问题结果返回(归)
    # O& u+ |( g5 a7 o7 s. Y  ]+ c; c0 a- d$ `
    注意事项
    7 R( D! D6 L5 ]  A5 h) [必须要有终止条件
    # f+ b  X! X. o) a& F递归深度过大可能导致栈溢出(Python默认递归深度约1000层)9 Q$ j, n( r7 t$ i
    某些问题用递归更直观(如树遍历),但效率可能不如迭代( B6 c; i: n0 H- ^8 Q- @
    尾递归优化可以提升效率(但Python不支持)
    ( R3 L* _- G: f3 H) o* @# d
    % v1 N& M1 L$ X: }# v 递归 vs 迭代  v  q" d- G6 Z( {4 K
    |          | 递归                          | 迭代               |. F! n/ x% |) K3 M3 x$ r" E
    |----------|-----------------------------|------------------|5 n3 V7 W" q7 w% R% m2 o3 o2 q3 z
    | 实现方式    | 函数自调用                        | 循环结构            |% [0 q5 {* r/ H! Z" A# z( y
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    ( H* Y4 m; ?( X2 V3 j! p" d; @: p| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |% A) G4 o3 x+ t/ l5 J0 n
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    " G6 J+ Z. M, P0 `0 _/ `# O' V. t& |4 P3 {/ p" d
    经典递归应用场景3 O# Q4 W" P4 b; V
    1. 文件系统遍历(目录树结构)0 ?  O' J  ?+ B* t
    2. 快速排序/归并排序算法0 k6 E$ E2 D) ^2 @
    3. 汉诺塔问题
    / S5 M' H' ^4 x( v# ~4. 二叉树遍历(前序/中序/后序)
    1 `: N" ^1 `9 K2 Z6 F: p5. 生成所有可能的组合(回溯算法)) @- x( ~! x8 |4 f# c+ k$ j0 B
    5 ]; _; E( [/ ?
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    开心
    7 小时前
  • 签到天数: 3188 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,, f  T5 a# Q& I) {9 u; a* f9 d
    我推理机的核心算法应该是二叉树遍历的变种。; O  f+ [' U8 k, d( q' u6 A
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    + h2 s& t, x( J+ ~# \- eKey Idea of Recursion/ h( v0 t2 Q+ N
    0 [  F2 L0 Y: L' l
    A recursive function solves a problem by:
    ( G2 E/ B, B, ^* `$ D8 B3 m2 W+ y7 ?. u' F
        Breaking the problem into smaller instances of the same problem.
    $ U# V8 ]3 Y/ g5 g. t$ i0 O1 `" L* x2 [+ R) M
        Solving the smallest instance directly (base case).
    - a9 S- h1 N/ G5 I( ~. w
    ; e5 o# n+ k. S! D. W2 y    Combining the results of smaller instances to solve the larger problem.
    ! X8 Z" X  [& i1 Z5 ^9 q" r1 X# U5 ]0 i
    Components of a Recursive Function
    3 m# E" w, _) U. w6 q
    2 e* n5 g* T/ ?1 f4 v4 l6 y) k    Base Case:
    * k$ k2 \5 F4 E1 w: r& C
    % O+ v8 _2 C- H3 T        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    4 `$ P, J: Z. d& c& Z. t: O  W) Q/ J8 M; b
            It acts as the stopping condition to prevent infinite recursion.
    & C9 v6 i* j- _1 X1 h8 T! {# @4 U. y7 j, J6 x: j+ v
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.3 L# p* R% w; Y2 K1 P

    / f, Q- s6 E, Z& ]1 q    Recursive Case:0 o: N& L+ j- v; a
    $ A1 Q1 M* M! ^% N/ y5 A, N
            This is where the function calls itself with a smaller or simpler version of the problem.$ I# m+ {2 I! h) x7 P

    2 j( U& Z2 L1 o# u        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    - A6 {3 ?& p9 C- h3 `0 d3 K$ A. e, n
    Example: Factorial Calculation' T3 R& m: o+ }8 W% H+ Z
    ; u2 J$ t, b3 _  [* g; E
    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:
    6 {5 L* J( T* G6 k* ~
    ; A* f( F+ |% R, V4 ?4 U    Base case: 0! = 15 B6 r0 P3 b6 O! N( }6 f4 Y

    # \" W8 e  f# c( Z, h    Recursive case: n! = n * (n-1)!' M! f& H. I" Z

    / w/ u2 c9 {. Y& X* ~Here’s how it looks in code (Python):
    % a3 b# p' f9 y6 N# Y; l0 Epython
    ! K$ v* S0 n' ?" U6 W+ h& ^; F$ c( S( ~5 M
    6 L" O2 q3 p  N' S% y$ N+ a
    def factorial(n):, P, }" m9 r; ]5 X0 Z5 x( Z7 Y2 V
        # Base case
    $ ~9 n" C, {; M; R/ |    if n == 0:
    8 y  o) |( T" p- t- w$ \4 o" T        return 1
    5 F: _6 A" ^5 y9 s    # Recursive case; g+ i" @" t; a; P8 j& }
        else:
    $ T' y' ^4 U1 N" U8 l+ M/ r3 |        return n * factorial(n - 1)* Q' Q4 {/ X% r

    " y4 P: ^5 K  E# Example usage
    ( H6 D/ Y3 f+ x( ]3 Qprint(factorial(5))  # Output: 120
    # ?4 Y$ J/ K$ I& Y+ i3 D
    ( J& a$ J) v* C0 Z+ ]( c. L  K0 iHow Recursion Works# F7 ?& ?; `; c7 E

    9 K5 Y* {. |$ ]4 _. G    The function keeps calling itself with smaller inputs until it reaches the base case.
    3 d4 ~( S0 ~3 h* H& @. C
    0 J. q0 k# M* l3 P    Once the base case is reached, the function starts returning values back up the call stack.
    8 [7 `' X& q/ o) r! c
    , m% z* n* E1 s% i    These returned values are combined to produce the final result.
    1 b1 `7 m5 k6 h
    ' s+ o4 ^3 I1 z* u6 cFor factorial(5):
    0 O  Y5 Z, A' L4 t$ ^' r2 t2 w. ~3 D( |
    , ]6 e( t0 z+ \7 e& o
    factorial(5) = 5 * factorial(4)4 H, r6 t: O% ~1 }2 D9 @% k
    factorial(4) = 4 * factorial(3)! A- j& ]8 Q2 N& ]& I2 z- k# O4 K
    factorial(3) = 3 * factorial(2)
    . G4 n- J/ z$ K( `factorial(2) = 2 * factorial(1)
      {  K' q/ [4 `$ K2 @! M( sfactorial(1) = 1 * factorial(0)* }- V, v. ^& x% d" P" o
    factorial(0) = 1  # Base case& N' l* @7 E7 A1 x* @
    . a4 O: f# T# b  [
    Then, the results are combined:# _2 g% F0 q! s% }" s
      B: s4 P  \$ i
    ( Q7 M% F/ h& M" Q
    factorial(1) = 1 * 1 = 1! q+ R$ j4 O5 @9 {* f
    factorial(2) = 2 * 1 = 2
    9 c; m- }( Z- R  ]: ]' |0 z3 Kfactorial(3) = 3 * 2 = 6, I9 X3 E4 o7 t3 ^. U; k  t% e
    factorial(4) = 4 * 6 = 24! |2 J" n$ r. P( P$ I, D
    factorial(5) = 5 * 24 = 1206 G  k& t. u1 n9 E
    ) @/ J$ K' @3 B
    Advantages of Recursion
    " h- i9 ^/ _; W! q* z9 o' Q; d
    0 u% x+ |( S6 K% E    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).
    ) m3 ]6 E8 @! C$ E- O* S* L; X% x
    # J: k* S* n. k% |; L    Readability: Recursive code can be more readable and concise compared to iterative solutions.8 [, K' R& Y% u! G7 r( ~

    4 }" M$ p# A" _0 T% G. g# W. a( {& ~Disadvantages of Recursion
    / |4 c1 l* T/ f# p  Z( P
    + q% U0 L9 E/ M2 `# G    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.
    2 e% x* ], K0 c& _! D$ _$ g4 W
      W, e1 ~3 t" F& R3 }" {  N    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).4 ^3 r  Q: A+ B" A: i
    8 ]) `! r. ?( X+ Y6 [4 {
    When to Use Recursion
    ; v) u5 [5 {: r% X* J& S( O: b+ L5 q$ p6 l# I) A0 E
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).: z  r  g' ?0 }+ {# v

    $ J/ m* s# t0 {! j    Problems with a clear base case and recursive case., U; E5 D7 u) g! J6 s5 j

    ) ?0 K5 n$ k$ i; P; O& x. a3 lExample: Fibonacci Sequence
    3 D0 a8 Z# {1 n" u/ A
    + l/ O* \5 m/ u: a" z% uThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:. |$ N' a% v- t* x
    ) Q% x" |& V/ c% c1 A- S" p+ m
        Base case: fib(0) = 0, fib(1) = 1/ b: Q: \0 X, B0 G8 u
    ; Q4 u8 O, b  q8 e
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    9 K8 p+ p+ d( b1 k  b( |4 K, q% I- B
    4 e% ?% I. g' |7 e8 ^9 s" Fpython
    * ~2 Y/ ]  E% E& c
    0 k' o* h. X) M9 Z1 H7 l
    5 R. a8 E3 T2 i8 Q6 ldef fibonacci(n):
    4 e# k) K* N- r7 R$ l    # Base cases
    * q5 g% P; m/ G( N, n) {5 x; U, d) Q    if n == 0:
    " @: J' v, L# o! H( e' [        return 08 J$ ?3 p# _2 n! f& L
        elif n == 1:
    6 K- G2 Z' n2 x- q        return 10 Y; F6 ~/ r; r+ X
        # Recursive case
    , r! b( O# ^+ V/ d/ C    else:# h: t3 D5 Y4 j; {7 `% @3 x
            return fibonacci(n - 1) + fibonacci(n - 2)" l  z7 s  R/ M) ^( A

      p( i* `/ t2 z9 v" F+ ^# Example usage* T  k9 N( e5 `- U% Q0 }# x2 [
    print(fibonacci(6))  # Output: 8
    ) {6 \& x* K5 u: e
    $ F7 n! J  z* [( P" k/ \Tail Recursion
    $ [- B- s- R- c# E( ]3 P
    $ u) P9 t& o1 ^8 [' S* GTail 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).
    4 m$ x" b/ T. Z& j
    7 ^* r$ J0 G# E3 `, N4 c  k" XIn 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-3-1 07:05 , Processed in 0.087729 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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