设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑   m4 s2 L9 p/ q& O

    ! d6 l5 G. L) ]& o  I解释的不错
    ' }- A9 I! x% z! a0 i! F. ?% B* |# n5 ]2 A' I8 |: g5 X1 Z) a/ ~
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。, @+ q/ l: m( z$ K# d% {* Z
    $ k, c  T- m0 B. p4 \6 g: P3 g8 c5 h+ ?
    关键要素
    : b; y1 k& O% G# ]' l/ |, K1. **基线条件(Base Case)**0 x4 I& C% ]+ |3 y9 y& l
       - 递归终止的条件,防止无限循环& D* i  W, B. w  Z0 N2 j& g! F
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1
    ) `1 G5 F, ?( i, q7 }! W! @. G7 y5 H9 r  ]7 v
    2. **递归条件(Recursive Case)**
    2 ?/ E: w" w' ]4 i! c7 B% D   - 将原问题分解为更小的子问题2 z% A- e8 ?8 m  B4 t  x- }, r
       - 例如:n! = n × (n-1)!% F" ~' z, g/ g' _# T* ]% R
    . `0 ]$ M5 p  D
    经典示例:计算阶乘, d& a) R! {* H, B8 F
    python
    $ m% k* q( Q0 M" F  vdef factorial(n):1 a. Z, e$ D- l$ L2 v. V
        if n == 0:        # 基线条件: F" l5 d! A/ ^7 A3 I# G: l
            return 1
    6 b8 b& W' u2 y- r  D    else:             # 递归条件
    " h- L3 w) K5 t        return n * factorial(n-1)
    ) N( A' c# o! O, z- }3 x执行过程(以计算 3! 为例):8 S- g, l+ N7 o& Y
    factorial(3)9 _% k9 m+ Y7 `  p
    3 * factorial(2)2 G& C9 q$ r6 b/ S" K
    3 * (2 * factorial(1))
    # P/ i5 c6 n. U* D( v3 * (2 * (1 * factorial(0)))
    / l! ?: X+ K) C7 y9 m/ w3 F3 * (2 * (1 * 1)) = 6
    # `( |7 g& I% M' s; y. n; Q" n  ?  M- y! e6 b% a! o7 t; V" x$ @
    递归思维要点" y. ?7 }# j/ u
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    . z6 M+ ]  D- M) }2. **栈结构**:每次调用都会创建新的栈帧(内存空间)$ `% Q6 e9 i* k- k& h5 c
    3. **递推过程**:不断向下分解问题(递)
    # P+ Y- o) k  _' d9 F6 A) k6 U4. **回溯过程**:组合子问题结果返回(归)
    $ w$ y+ y, k; w7 K# o/ f; A9 Q! H# g1 I+ t. s) \
    注意事项
    7 E2 W4 v! b: N$ w7 O/ j& x6 g必须要有终止条件
    & D& g8 H& y8 M8 A: B# Z递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
      c+ _2 `, @, i5 R+ [) M某些问题用递归更直观(如树遍历),但效率可能不如迭代1 k1 u; I8 t1 L
    尾递归优化可以提升效率(但Python不支持), y0 J% t7 ]( H1 U: A% Y6 _

    3 `% o& `& r5 G; c7 n: n8 P! v 递归 vs 迭代
    & G) b! U7 f9 v9 ?+ u" G! t; U|          | 递归                          | 迭代               |
    % v9 E+ G: U/ J( A% U4 R* o6 a8 y8 H; N|----------|-----------------------------|------------------|& Q* ^$ w( X% m7 _' G% {( _
    | 实现方式    | 函数自调用                        | 循环结构            |
    1 P6 F% h7 t0 [& O" Y7 [| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |1 @* j" N, `! V7 D
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    & I/ q2 r+ \4 l+ B- e5 y| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    - t$ e  M7 T( k, a) D. ?8 Y" |+ t8 p; y. ?7 ~8 q" y
    经典递归应用场景
    2 z& ]+ g' ]7 i" `0 Y+ q1. 文件系统遍历(目录树结构). Q- O1 f. J8 O; z( p
    2. 快速排序/归并排序算法: \; S! O1 f% f. c+ i6 M
    3. 汉诺塔问题+ J8 T  ^7 u, B0 u5 S
    4. 二叉树遍历(前序/中序/后序)
      s, c. P4 i( r5. 生成所有可能的组合(回溯算法)6 S( v& z3 j; C! s

    6 F1 A- R, G' F3 Q  L& r试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

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

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,7 c  ~$ Y$ F& o8 Y/ {* `
    我推理机的核心算法应该是二叉树遍历的变种。* R+ r8 C* N+ Y1 w/ D6 w
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    + c# l, V& t8 d  rKey Idea of Recursion
    9 G! m* ^  c" l$ G0 z/ G! o6 I. N, H4 g2 Y
    A recursive function solves a problem by:
    / g. Q5 a  A1 [3 {- ]- C' Y. h  |3 F6 Q1 E, x# q! D0 O) Z% @7 ]
        Breaking the problem into smaller instances of the same problem.  [; \+ G- E+ I
    9 w/ {6 I9 a9 H+ u# u
        Solving the smallest instance directly (base case).
    7 U; H9 p2 ~) ^: O) X1 t
    0 z4 K+ {+ M2 P7 E2 Y; c    Combining the results of smaller instances to solve the larger problem.
    % s4 _9 f. i, p6 c+ G( V5 w  K" s4 n8 n' V9 P8 m
    Components of a Recursive Function
    ) h$ z" M: b( T1 ?- M4 j& ]  Y. M3 o4 [. I8 J# f
        Base Case:! @: }7 [- z% r+ o% T1 W8 x- w* n

    5 ]5 n  Q, u# V  m! r! t        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.' u! B5 r) y1 S1 T; S9 b. c5 N
    $ D) i, }/ L' ~+ }# ]
            It acts as the stopping condition to prevent infinite recursion.
    7 N( [. Z9 L% z6 l* p( w0 {$ K1 H# j$ a8 {9 N; t  K
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.! Y7 |% @/ N) U( o  I5 J( D
    & c' U' c1 d- z' d* S; p
        Recursive Case:
    % i- d0 u: Y! p  D+ H. D0 D
    # S) ]: @8 j1 R( i$ s5 \, t$ a        This is where the function calls itself with a smaller or simpler version of the problem.
    ' F. }  O, K& m) n' J+ E
    " N0 t( d+ p7 Y+ w" ]% X' b        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    , f& n4 Q$ l# ~5 \' P
    ) j# r5 e4 g  N$ ZExample: Factorial Calculation
    - w5 t6 ^$ {; u: @: [* _( K: h5 z4 A6 ]( H8 g& e4 F+ n3 i6 _- z
    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:" u. P3 q; G( L" w- e4 e7 s

    1 P" H+ d+ |( H2 {8 y    Base case: 0! = 1( v# n2 r5 [" b, B: ]7 h8 U

    ! B- n$ K9 E9 C" b    Recursive case: n! = n * (n-1)!
    % }. e: X8 \# g, w2 ]4 i
    # C- ^" p6 ~  a  x3 u! \Here’s how it looks in code (Python):
    , t2 t3 O5 Z  t! a# U- d( _" w% fpython5 ~/ b& }3 |3 t% C
    # C2 `: h3 m5 [: f

    , Y! D9 }* S: S, w% ~/ k4 Hdef factorial(n):/ z3 b4 f7 R9 C3 H, z
        # Base case% `; i) k( d1 U
        if n == 0:8 J& _+ n5 ?# t* w" L
            return 1- y6 J, p+ k# H6 l5 j; U4 X" ?4 {8 n
        # Recursive case" O1 \1 l2 N6 a' a- K
        else:- [) [9 j/ r+ `1 o) Z6 }
            return n * factorial(n - 1)
    6 E6 {8 \- A% w3 b( |3 c, E: p: D# s$ c* e
    # Example usage
    - n3 O! N' v/ |. y' Y; V" x' x& B0 cprint(factorial(5))  # Output: 1205 {6 h+ m& D. L0 G9 d- C

    ; y7 B9 r1 Q2 n2 Z% J  ~How Recursion Works6 A. k" j6 P  S

    1 d3 h* u; e: d0 _* f- C" g- ~    The function keeps calling itself with smaller inputs until it reaches the base case.- l: Z( Q, j1 O5 o: r, ?, ^$ a
    & w3 L: d9 U  j" {
        Once the base case is reached, the function starts returning values back up the call stack.- X- Z: X! M0 K# w* C! s4 R

    ! V0 f7 |7 C+ J& a9 F( S    These returned values are combined to produce the final result.
    4 g% y3 c' g" Y( w# b1 H0 R
    " @! l6 \. p) @- L7 J) uFor factorial(5):
    ' z) ]7 `8 a$ @( L. j- A6 m5 R6 k( X5 {# a7 C1 i

    4 y# s  [& i1 G& v% F, Ufactorial(5) = 5 * factorial(4)
    / y( q/ t9 R$ U$ ufactorial(4) = 4 * factorial(3)9 s0 p3 w# X5 z" g( ]$ n7 T
    factorial(3) = 3 * factorial(2); h# a4 K/ J# G. o% F4 u
    factorial(2) = 2 * factorial(1)
    " B' n* a. H- I) x3 r: Sfactorial(1) = 1 * factorial(0)7 X1 p4 m8 ?) e7 w3 U# q- I
    factorial(0) = 1  # Base case% f7 I7 L$ u( O, p

    9 t8 l* M, f: o0 ^Then, the results are combined:
    % P/ e% P$ ?7 N) ?: W
    6 ?7 G$ t% s7 v. o* v8 ]3 @; g% z/ `& q5 e  u
    factorial(1) = 1 * 1 = 1* I+ {5 B, T5 ]# ?9 |* c
    factorial(2) = 2 * 1 = 2% l7 \  E6 B: d) T3 l3 Z; ^
    factorial(3) = 3 * 2 = 6
    % W8 Q9 l% r% B4 A/ F; zfactorial(4) = 4 * 6 = 24
    ; a- m% i5 N, W1 G0 W+ Dfactorial(5) = 5 * 24 = 120
    " t, [" G  o) \5 L- v& z0 N# a( d6 @: v$ E  C
    Advantages of Recursion- g/ D& |: H' E
    # A4 S  q! ?# P, _% y6 t. l
        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).& G( q' A& h2 O. S$ O/ H

    6 {! n2 D) L# o- ?5 @. ]    Readability: Recursive code can be more readable and concise compared to iterative solutions.
    4 C) f1 t$ r, H2 N9 y2 b/ n1 y* r4 D% A
    Disadvantages of Recursion
    " s& i9 ^. {. p3 d1 T- H# i9 s  N6 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.7 _& M5 c% A! x7 w# B. x
    & H0 q0 H: Z: s/ E0 s3 a
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    + S: E2 Q* S5 E9 `! e, T0 }: {4 c3 F; l( N/ i
    When to Use Recursion+ q. e% k" J3 W9 d4 h* `

    ( Y' M# P- ~1 @- i4 Y9 A    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).2 y) p0 ~: C- {

      s" H8 s5 t" p  J    Problems with a clear base case and recursive case.
    ' i/ C; g0 i! m% E) T1 j. d7 z6 z5 q; I9 L. F/ z' W
    Example: Fibonacci Sequence
    ' c1 r. T2 n, u  F) O  G7 T3 N4 i& b9 f1 d- A
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:  a) X) A5 p( b" V! E$ _; m
    0 r; R& ~; @3 V8 u# t) ^/ o
        Base case: fib(0) = 0, fib(1) = 1& @8 R9 |2 }2 u. U

    " ~# [: B% G/ y/ Q( ^* K8 @* W, Z    Recursive case: fib(n) = fib(n-1) + fib(n-2)# u: d, J/ a3 x5 g2 ~

    3 D2 Z9 i% y6 {" i2 x5 C, zpython
    9 Z6 E, q$ S0 X* |: q" K' \* Y, h: G
    ) {4 i  `: n' T1 ^' Y1 V' E" M, [7 ~4 L! m" M( P
    def fibonacci(n):0 l& N& z0 l  q. K
        # Base cases
    # `  {2 }  n6 N) I    if n == 0:2 }2 N7 _# X' A; _' e: H
            return 0
    ) O; u( n7 K" Z7 Q5 G7 s4 j8 z    elif n == 1:
    / H( D& _1 G5 n3 J        return 14 m% @" }( M9 N: }; ^# S  q& T
        # Recursive case
    9 [; h% n* o. `& M    else:0 h8 f6 o( T- x6 V  L
            return fibonacci(n - 1) + fibonacci(n - 2)
    1 J( j9 s/ M7 T* f: l' ]: Z' I3 \! J0 S  W; L/ i. U
    # Example usage
    : g+ L5 t9 ]' }print(fibonacci(6))  # Output: 8
    4 k7 x* ~8 p0 J/ D/ s% E! q
    , r# P" l. B) i; e1 PTail Recursion2 A1 {, C0 J6 S4 W/ X) b
    9 ]# P+ X0 R& D% [) z
    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).( S* ]4 M+ f- @: g7 x4 }3 ?6 q0 Q/ A
    . H6 s1 t* |# }7 {
    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-11-24 10:19 , Processed in 0.033862 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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