设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    9 q4 F. S+ g$ K$ ?6 o# l8 d# A# V
    " N" g3 n0 h) m解释的不错
    ' Z6 J: O6 A; f6 ~) `! S. S( F4 S1 a3 x9 x& ]4 \/ }
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    - z8 M" u: N3 r  w# b) z; [/ j& D" H* h1 X
    关键要素
    / E: q, Y4 e6 w1. **基线条件(Base Case)**- L7 A/ y3 O. Q, q; B& d
       - 递归终止的条件,防止无限循环8 Q. D9 M! b7 G5 ]' C3 _
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1$ G( J7 t0 |0 Y+ y9 b  X, A8 K, q

    + v: J) H1 A/ F: A* r+ }2. **递归条件(Recursive Case)**
    2 Q1 w. y4 A( }( G" v+ S. R( p" b  w   - 将原问题分解为更小的子问题
    4 O: g; x. h- S9 C5 p* i! F. O% ?   - 例如:n! = n × (n-1)!7 B5 {/ Y$ f1 ]- S6 q! n
    5 a5 ]$ e9 w6 n' m! R
    经典示例:计算阶乘
      M- U$ ~( C/ X! {$ C# ?python1 O2 `. @" c+ a" J4 s! t, x
    def factorial(n):) r' A" N0 _# C" k# A
        if n == 0:        # 基线条件8 i$ H, O; F: W  |  M7 `( t: U) L
            return 1
    2 j4 S6 p" D5 B    else:             # 递归条件
    , m' m+ ~& B$ P- ]        return n * factorial(n-1)
    $ O& v% j, X8 q; O& |" Q执行过程(以计算 3! 为例):1 `, n/ d0 \5 ?3 f* R+ w$ N2 X
    factorial(3)
    ; D, C* i# l# m3 * factorial(2)
    9 k( W) u1 O' g# S3 * (2 * factorial(1))
    9 N; s" c4 W7 D/ Y3 * (2 * (1 * factorial(0)))6 W" h. w: c8 j# o
    3 * (2 * (1 * 1)) = 6
    1 ]: v9 v, a$ a* y! y. ~3 y6 x& }' F6 |9 z4 p' v0 q& \, ?2 e' J
    递归思维要点! |, p! d8 v7 s7 O
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑, I/ X7 j' L9 j) y7 m
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)7 b, f3 M6 B$ g5 J6 J1 Q
    3. **递推过程**:不断向下分解问题(递)
    ! a1 Z; l: O, `3 r( X' @' I& B9 c4. **回溯过程**:组合子问题结果返回(归): f& a1 V  s7 l- t, W

    $ L& K! k1 v7 ^: X6 y9 i 注意事项
    . E/ a0 e3 ?. P& b必须要有终止条件3 P7 B! h* x' X+ ~( e( K
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    ' C7 p7 u$ c$ k某些问题用递归更直观(如树遍历),但效率可能不如迭代& n) {+ j* G9 K- a: O' L) u' I" V1 t( W
    尾递归优化可以提升效率(但Python不支持)0 f7 w7 H1 l# }) l. l# f) h* n4 z4 a

    " p( W7 G3 R9 g3 o# M" b$ d 递归 vs 迭代6 t* W  O2 T1 K, E' l" f! {
    |          | 递归                          | 迭代               |- w' s/ C4 O2 o  S" T
    |----------|-----------------------------|------------------|2 _. g4 q$ b! n( b9 d  e
    | 实现方式    | 函数自调用                        | 循环结构            |
    " _; o  c4 z6 T3 X| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |' ~9 G' G) r( E9 L' e& h& Y
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    % y  I: J/ B! J* i  N| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |! o4 r) ]3 C" U: w0 O9 w# Y
    % ?- g: H7 t1 N
    经典递归应用场景
    2 l: k' v) ~* Z( L4 I; l1 H; x1. 文件系统遍历(目录树结构)
    ( g2 d7 j- R2 P2. 快速排序/归并排序算法3 M1 U( K8 O1 ]2 n; U! g
    3. 汉诺塔问题
    5 @. }( ^0 q$ I6 H% V7 V4. 二叉树遍历(前序/中序/后序)
    ; ]0 P8 p6 @$ i& o6 c2 o  V  c5. 生成所有可能的组合(回溯算法)
    ' J" H! t( _2 A- X: |3 v' ~2 |7 i1 h
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情

    前天 07:45
  • 签到天数: 3217 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    + a$ ]+ q, X5 A- }3 Y% I. X4 F9 E我推理机的核心算法应该是二叉树遍历的变种。/ y7 r: G8 }4 S! b
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:5 `9 Y4 T* b, O- I- m8 G2 S3 ]
    Key Idea of Recursion2 j$ c4 u1 f9 b9 q/ e% b

    ; c$ W, `; \' S( z, `% X9 ]A recursive function solves a problem by:! u$ a% f/ K* F3 T2 }4 Y) h
    - u3 L/ r/ u! ]- O
        Breaking the problem into smaller instances of the same problem.2 }0 T) t0 m3 P# V; |2 ?

    / y0 z2 W0 {+ g4 t+ h* K5 r    Solving the smallest instance directly (base case).
    7 ~+ T' u% B% j. i2 f' ~' h& a
    # i. j& w# s. P& L    Combining the results of smaller instances to solve the larger problem.
    6 W; j5 o2 u- _& h! \" D/ H3 @. |/ p% M+ S7 O% T6 }4 l
    Components of a Recursive Function2 X4 K: z4 A8 \: I+ T3 c, m
    * [# [$ K  x0 E& J! L# a
        Base Case:8 y) R1 j3 Z+ a; ]6 ]

    0 o; M3 G$ f" _& J6 ^        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.6 v% P1 F8 G" ~' d  _4 h/ M- I
    ' B/ K( l$ ?  e' H( a, j3 I
            It acts as the stopping condition to prevent infinite recursion.
    # r5 Z7 F. A1 L' }, h5 `8 s$ e# W- ]& j; l' G; s  \1 ?. m0 u
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.' f1 {3 C; D+ y& K1 E! d
    * B; z9 [- M+ m2 o: `
        Recursive Case:5 T4 u& C: t, T" ~9 y
    # \9 b' D2 k$ I+ n( I0 w% v) s# u$ Y
            This is where the function calls itself with a smaller or simpler version of the problem.
    : }2 I6 @- x5 f* D7 O0 }6 a( a2 a! }' |( v& x
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    + [( x$ J" O, _8 y: I; ]8 G9 o* ~0 i/ |9 E7 I# Q" I7 ?! z& o
    Example: Factorial Calculation
    ) L, {# y( C0 j. @; ~1 l7 l( R
    ; K9 S; D' _1 RThe 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:
    ; v: n/ x% a! I7 ~1 o2 N+ r
    7 T; p5 ?: m2 q" N' ]    Base case: 0! = 1- t0 T% E7 ^3 {; ]6 z
    8 c2 }) h4 e! p& q; ~
        Recursive case: n! = n * (n-1)!3 a* g' r' E& D' M1 s3 w

    . m8 \9 _1 M8 U- iHere’s how it looks in code (Python):9 M. J7 E( i1 z: ^9 @& `4 ~
    python
    . }5 H6 d% T- n0 f; m' S9 f
    9 g+ I3 h) T1 w* c( l0 G; h
    4 K* j/ g$ ]9 p4 Gdef factorial(n):* b1 i* B: p( b% \
        # Base case( q  v: c3 J0 C. W* j' I* n
        if n == 0:, L" X: B% N+ v  E, p( T6 S
            return 1
    0 V9 W1 n- C9 ?$ b    # Recursive case2 Z! \8 N1 I; g+ ~
        else:- c- ^& Z% |, A. h8 N0 L) i
            return n * factorial(n - 1), c! T% o+ e. [6 r$ K. r
    / E9 e/ x5 o9 j" i) f3 K7 d
    # Example usage) t- ?( T  P0 |; X0 [" i
    print(factorial(5))  # Output: 120
    & ]3 b- p7 }$ q8 y  \5 J( O  ]1 F: c/ y
    How Recursion Works
    0 v8 j4 S4 L/ j5 }: @4 B+ |3 v" k! b
    1 L% R9 r/ W& _6 I5 c    The function keeps calling itself with smaller inputs until it reaches the base case.+ {* P% D! @7 T- Z
    $ N& t6 p" H2 N  F& {1 f, o* k5 A
        Once the base case is reached, the function starts returning values back up the call stack.
    . W1 u3 K1 `) f6 @7 M8 a- q* L. U& i' j# k
        These returned values are combined to produce the final result.
    ' j& A" v0 f) m+ c: w$ I' m: v* {8 \. P  |7 P
    For factorial(5):
    1 H9 K$ K$ ?# z. E1 c& d! p9 t
    8 S1 e: x6 G6 P* X3 ]& k3 ?  A8 i' \4 m" N
    factorial(5) = 5 * factorial(4)) K- [# Q) J3 x, G6 g
    factorial(4) = 4 * factorial(3)
      ]7 N% j0 Q0 F& Z2 mfactorial(3) = 3 * factorial(2)
    ( V: {( E2 a' U& Zfactorial(2) = 2 * factorial(1)
    + l7 g. D. i" M6 A% v! dfactorial(1) = 1 * factorial(0)
    ! a7 O) e( G6 C: ufactorial(0) = 1  # Base case  M3 c, B- a( L+ c6 f' M

    0 Q" i: I2 ?; x) p( d- }, ~, FThen, the results are combined:
    1 l3 N! H( H3 E) E$ C/ g0 l7 c! S8 k7 @  W: E
    / S( i5 t4 ]% i  U% D
    factorial(1) = 1 * 1 = 1& L# v( C) X2 l* F, r
    factorial(2) = 2 * 1 = 2
    5 v: y2 q% O: O( J8 E7 ~factorial(3) = 3 * 2 = 6
    $ t! ?( p" z) G1 J4 dfactorial(4) = 4 * 6 = 24
    ) P% Q. y" W' [  n# Z+ ]factorial(5) = 5 * 24 = 120
    # Q: j- t! v( u( d" d' ^5 M0 `; y9 t
    Advantages of Recursion2 Z3 s+ E$ g) Y' h

    ) l% ~3 J. {) l* ]7 R+ B    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).
    $ L% |4 B0 j) d
    ) K# R, {7 J( C$ t    Readability: Recursive code can be more readable and concise compared to iterative solutions.' D6 I5 K8 w  f$ `; e
    9 U# F/ ]  s  l8 n# X
    Disadvantages of Recursion0 m9 R5 v& u& m: y' O8 r# j; x
    * ?3 D$ x# \- X% {- K* |8 Z! F3 F
        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.! X6 c0 O6 ~9 o! {; W- Q* F

    9 ^% [' M" X( m' [3 O! N! q, E: q    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).5 L6 |( V+ a  L
    ) I& G6 t. ?$ N5 |. [$ B
    When to Use Recursion/ X  ~# {! r. d; i$ {% Z0 ]

    6 k/ P/ G4 A" f0 m5 j' M) v    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    ! g# s! \' f% [# s% A
    6 U1 |+ ?$ j+ o. B    Problems with a clear base case and recursive case.3 W* X3 R# V0 Z

      A: c3 k$ X1 @Example: Fibonacci Sequence
      ~! p/ F4 E" M4 P0 R4 x& {% v2 S6 \3 X' k$ `
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    ( {6 x$ f- A/ l4 A3 v5 \- y$ M5 B. l; [/ \! I# Q
        Base case: fib(0) = 0, fib(1) = 1
      j; k" R* r5 `( {2 W, h
    0 y  q/ \$ \1 X* N# D4 t4 _    Recursive case: fib(n) = fib(n-1) + fib(n-2)
    ; I9 d' y* l  s
    ) C3 ?0 t  d" L+ Lpython
    " G+ Y  P6 b2 t2 C) Y7 R; {6 n2 e, k5 N. w
    1 ]; a0 P; s* L8 a
    def fibonacci(n):
    & d& ?8 s* u0 O5 P. p    # Base cases
    % B1 y% M9 l4 J  l    if n == 0:" n0 h& _: a4 D6 M
            return 0: _! {( g( i) S3 _& Y
        elif n == 1:9 R* o* s' N3 U: N8 p% L0 n
            return 1
    3 h% X! v6 R' s) n! |$ u9 J2 ~    # Recursive case8 z4 M! v4 J8 U" c) L% z6 n+ x* k, l! j
        else:4 h4 a+ b+ H  Z" Q
            return fibonacci(n - 1) + fibonacci(n - 2)
    * }' V" K% ]9 g9 y( [& \8 {) v5 |7 h3 ]& y
    # Example usage. X8 _. {7 R% T, f
    print(fibonacci(6))  # Output: 8
    ( `- m1 U; m% z" S
    ! v" M( r6 O& E# v( jTail Recursion  B1 U' z5 k; N- K: V( B$ ~8 f
    . L9 R$ {$ _4 n) C! P& I0 l: d
    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).
    ' r* q0 l2 U5 w9 u% I# B+ X. W1 @/ a) U, |( ?" m" ~
    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-4-11 00:12 , Processed in 0.058624 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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