设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    / o& P6 @  x7 Z: k8 ~" V% v
    : j& d% C( }7 h% w解释的不错
    . a! p% V( o9 s9 e' u- c8 O& f  i3 S; r7 E
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    4 v5 R: o) `6 @4 W: |3 Y1 l* N& ?5 }
    关键要素! C& D1 X) `0 \, w
    1. **基线条件(Base Case)**
    & I5 I" z( Y# ]$ ~  |   - 递归终止的条件,防止无限循环
    - B2 ?. ~& _6 h& m8 s   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 16 B$ b/ o* N- r4 F. r  H2 P2 }

    ; m2 D: R! m* ]( }, Z% Y2. **递归条件(Recursive Case)**
    7 O9 C; a0 k) h+ S- U  m8 @$ V   - 将原问题分解为更小的子问题
    ( a1 ~# C- E- y6 k& u   - 例如:n! = n × (n-1)!
    0 C" s3 v" m3 \$ h. W5 ~, Q# C! L! C. L- |1 b$ d
    经典示例:计算阶乘
    * p+ @; W- ~+ T2 p: lpython& m0 ]: C, Y% Q/ u$ G
    def factorial(n):
    ' Y' i  V7 a5 \! x$ W* ?! w    if n == 0:        # 基线条件
    7 B4 E: Z, p, ?& V+ D  k. s* w        return 1- o. K4 f: i5 t, Q" t1 S
        else:             # 递归条件: @5 b8 {1 R7 Q% {% g% l6 F
            return n * factorial(n-1)" Y( J8 t  z: k! s
    执行过程(以计算 3! 为例):9 F+ c& m. O8 Q' W) D8 F! L8 `
    factorial(3)% J2 V4 Z) `' a
    3 * factorial(2)  ~4 R. o# J1 p
    3 * (2 * factorial(1))& y0 L" }. h8 d/ m" `' {: r
    3 * (2 * (1 * factorial(0)))
    , V9 [; ~' u4 Z( u3 * (2 * (1 * 1)) = 6" D/ _* A1 V; _3 L

    1 w. C2 T1 L6 e' l 递归思维要点4 L; N/ K# Q3 A
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    3 F3 l$ u5 N* D0 r2. **栈结构**:每次调用都会创建新的栈帧(内存空间); F% x4 |" J, [. Z. E
    3. **递推过程**:不断向下分解问题(递)
    1 L' b+ k- j. n6 j2 V' c+ E7 I4. **回溯过程**:组合子问题结果返回(归)
    % |" G) C# x( C8 t2 z: a" F* X7 d: d4 ~
    注意事项! C  S* v9 ~6 E) U/ Y/ M2 S6 t
    必须要有终止条件% N6 E% |8 R! U0 W, \0 s% F  Y7 }
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    " c1 f9 z, v5 R某些问题用递归更直观(如树遍历),但效率可能不如迭代3 K% m5 x/ ~* q9 _6 A
    尾递归优化可以提升效率(但Python不支持)
      g' m4 U; U- o8 l( v5 u7 }% I  v  p9 p$ O2 e+ u- E( b! ^
    递归 vs 迭代0 @6 r1 a' }0 i9 C
    |          | 递归                          | 迭代               |4 Z, u$ z- T% X6 k/ h7 V: B0 j  M9 `" g
    |----------|-----------------------------|------------------|
    & ?5 n3 w. ^, L% M6 K. w| 实现方式    | 函数自调用                        | 循环结构            |' P6 t7 A1 M, l, M/ D$ p9 A
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    5 J. M% A% f& f2 O| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |% P" p8 m: q# K: t( g. }. s
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |( F% J5 m! F- }* z, P

    . ?' C: i1 T/ x0 d 经典递归应用场景
    & i0 \7 @. G5 a* Q# D; z' D1. 文件系统遍历(目录树结构)
    2 E/ C' R& J" {+ `, `! S2. 快速排序/归并排序算法& ^2 I2 C1 b  w+ K
    3. 汉诺塔问题
    % q; ^' g1 |5 ?& U' M) j2 Y4. 二叉树遍历(前序/中序/后序)
    / W  b) z9 F# ~5. 生成所有可能的组合(回溯算法)
    ) L( K$ a1 E& o) n+ Z& ]3 X+ Y* \) O1 U: W
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    无聊
    昨天 08:02
  • 签到天数: 3126 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,0 K' p: H6 e1 c
    我推理机的核心算法应该是二叉树遍历的变种。% w; `- S+ s1 O
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:2 F1 }+ @9 I: I8 T+ L) [* ?: Q
    Key Idea of Recursion/ |5 r9 w0 z5 O  V

    $ @3 Q. K8 q1 S" k5 k1 p+ m1 k5 iA recursive function solves a problem by:
    ) S- c- M& r% W( `6 q5 n, M0 B( z2 e9 l0 J3 ]) @/ O9 n1 @
        Breaking the problem into smaller instances of the same problem.
    / L3 q# l: b5 o$ U" b# ~
    2 V& s; z% Q; C; ^9 }1 r' r    Solving the smallest instance directly (base case).
    $ H5 d; B  S& U! `' g1 t% v
    % e& \0 O: m9 ~4 Q5 y, }6 G    Combining the results of smaller instances to solve the larger problem.
    # f6 Q+ [4 H3 M4 N! Z/ |: b% R* h6 T2 A8 t7 a8 \# [" Y1 i
    Components of a Recursive Function
    2 a5 Z( I# E5 b; n3 N) G' U3 S- I, X$ E$ z9 u
        Base Case:9 g8 z* z! V% \) j$ Q4 g+ d% x  b
    . d* q3 @( P5 Q9 @; j) _; O" V
            This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    . E  y& o+ W  ?4 D! w( p2 T! w8 R" H' @5 q% g. F, {. S
            It acts as the stopping condition to prevent infinite recursion.
    # ~6 `% s3 e  e, N8 d0 C! J( H0 m+ d# j% h: ^
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    : y( R  l+ P% {) f0 {
    : x" F, o; r; |! P. b    Recursive Case:' k) c3 Q8 L- F
    , u' S* A7 J9 e* r* [& }! B. t+ D
            This is where the function calls itself with a smaller or simpler version of the problem.. C8 Y$ O! C! Z
    ) }( g" B( p' i5 S/ p2 i8 h
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    7 q$ i( z% P+ g' V7 H
    / }& v) j9 I! K/ q$ X! I& T& m: lExample: Factorial Calculation- ~; h2 u, X1 g9 T

    * _3 m& q. j. \' C5 a+ g, d* ZThe 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:- t# A% _" k5 {8 D# @# d
    - n2 Y6 l# x0 |+ }9 Y
        Base case: 0! = 1# D# \" ~8 _7 H$ V9 P
    - W* X/ Z/ S* B- t! L
        Recursive case: n! = n * (n-1)!
    ; `2 ?7 E) t; F5 {! X3 i
      L6 ?5 Z4 e5 F' |: @& NHere’s how it looks in code (Python):, W6 f. d4 F* Z8 E" h
    python
    , W  @0 w9 j, W; d/ a9 f# W
    7 r- G2 b0 }2 p" T$ U7 \# O
    , }4 @. D$ E4 e! |) {* m2 ~def factorial(n):
    1 M& Y- V8 g+ M- _7 }! y7 \    # Base case
    . s7 i4 F) N4 T9 S- G; b+ Q( Y+ ~, n    if n == 0:: @( R* Q0 p! e& S  W
            return 1  S9 `- n, e+ v  w- ?& F
        # Recursive case
    8 g9 l! v+ J* P( ^, l    else:
    0 I" x( v& Y- u: e        return n * factorial(n - 1)- {4 Y8 L, b7 v! ?7 P
    8 h9 I4 n/ {0 w' I1 T# k1 \, k
    # Example usage8 v1 ~  T: Z5 h" q
    print(factorial(5))  # Output: 1202 s) d3 x7 [) h

    5 L& T  U- t; g$ I1 P0 V5 t9 V+ PHow Recursion Works* ?: j, b+ K' K0 n
      D% y* |) @2 B& A+ E. ], {$ k& Q
        The function keeps calling itself with smaller inputs until it reaches the base case.
    ) r3 j: ~# X0 y
    / R  z: Z) ^; z; e, S) w) i7 @    Once the base case is reached, the function starts returning values back up the call stack.
    ) e% T! e5 M% g/ w7 q. g% N, l: g# |4 y: o' f
        These returned values are combined to produce the final result.
    3 c+ W  D* L! h4 F! H6 v2 q, c: ^# Q$ o( o0 j2 [9 Q( ]
    For factorial(5):
    7 m% C7 K/ U# s3 G" I; e& f4 ~2 v9 \8 Z+ }  _3 G

    ; b- t0 \+ D9 h. _7 R8 j/ Qfactorial(5) = 5 * factorial(4)7 d1 Y4 [* _( k+ w: |7 j/ e0 x
    factorial(4) = 4 * factorial(3)4 w! C# R7 v# T: ~
    factorial(3) = 3 * factorial(2)
    : V2 r' M4 A, k- Qfactorial(2) = 2 * factorial(1): t# p: L  b3 t  I% {3 ~" W  U& J5 F. g
    factorial(1) = 1 * factorial(0)
    7 V" ^  E% t/ Ufactorial(0) = 1  # Base case
    & A8 B  W2 @, b/ L; I
    + q5 q. |0 l$ `' f/ Q$ GThen, the results are combined:
    ( Z2 |/ G9 s$ Z( e' Y7 d  N" S, u! [& W% a1 ~& J

    % I" t% S: Q0 @- Cfactorial(1) = 1 * 1 = 1
    ) h& H: ^; @8 m, T6 q# Ffactorial(2) = 2 * 1 = 2
    * D  J* f4 v# D' h+ I1 p. R) gfactorial(3) = 3 * 2 = 6: [& D0 k% o' T
    factorial(4) = 4 * 6 = 24& A4 G9 [) s' w2 X
    factorial(5) = 5 * 24 = 120
    7 j; o) \: A$ p5 ^+ |' L- K! }. l4 B% h1 `7 H: `- g* B
    Advantages of Recursion
    - J! X3 ]  {$ T5 S. B' Y7 X8 |
    5 n) T4 a0 o1 X0 H1 k( Q    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).
    $ S! q3 e7 i1 u1 M+ }: e! l& S
    / u7 h7 G! j- M6 h" i    Readability: Recursive code can be more readable and concise compared to iterative solutions.+ n1 d- ^8 N8 S) A0 H4 ^1 h! J

    0 l5 P" q! v* V# q5 m0 p; c0 v5 ]Disadvantages of Recursion* n, K9 y3 w, [1 w+ D! K

    , T  j& D: y$ V. ?( V- x    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.
    / ^5 `7 W- @; G
    6 C; z9 b+ t+ x( L  F! c, P1 K    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
      T6 D3 b& L/ O9 t) T) Q2 X- P
    $ R6 k; M+ ~/ M' N* J0 q) _When to Use Recursion5 ?- F( o5 P! h4 W- D
    ; a' V+ b7 a! e6 r: R/ r5 y
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).0 S% |7 n2 Z4 }: e3 y+ z1 _' U. I
    ; k/ e4 e1 d  i5 ]
        Problems with a clear base case and recursive case.
    6 ^! {1 C( d( X" n
    ; R7 D; a4 j; r2 t( zExample: Fibonacci Sequence  e% |+ f# t% p6 O6 A

    # s7 T# ]: Q$ I, [  ]The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    # R5 Z5 g  I" }$ t3 o7 o0 R/ g, [, d) {" \) V6 m
        Base case: fib(0) = 0, fib(1) = 17 s) s- ?3 |  S& z! e6 g
    1 e( K6 Z6 o5 d: H4 W: T8 |
        Recursive case: fib(n) = fib(n-1) + fib(n-2), _% a  c! B" N1 \; W

    0 Z3 s+ O2 N$ Vpython
    ; }! e4 ?+ Q$ [, S5 X) O9 m7 K, D$ }6 f& S
    3 |8 x' X5 E) M) y% N9 @: _3 t1 d
    def fibonacci(n):4 B( f! z1 Q1 `
        # Base cases
    0 p# K$ R( S( h3 k& A1 c    if n == 0:
    5 H; n: Q' O( n0 C- V0 f* m        return 0
    / e& x* u% z; r' p( d    elif n == 1:
    * p1 P& h+ l. B2 C# p. p$ w4 a% h        return 1
    3 Z* G5 W8 s) u5 V  V: H- ]! p    # Recursive case7 j1 Q  Q! y6 D1 Z9 ?4 Z4 A
        else:
    7 z: |6 P+ _# ~- C; ?        return fibonacci(n - 1) + fibonacci(n - 2)( G, p& V  j1 X* J) g7 A9 m
    % ~, z" a# z& w2 H* \' w
    # Example usage2 \3 k; ]" t/ J: K/ J+ v
    print(fibonacci(6))  # Output: 8% v  t5 Z, z* G/ h6 n* W

    7 P& t) }% b* b+ dTail Recursion# k1 U+ y2 i+ u5 E9 E

    4 B- a8 N- o6 h6 R; Y- eTail 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).9 @4 ~9 W  \/ P5 J+ b* i' e/ C5 x
      k6 |1 q4 e& Y) l# Z0 o
    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-25 08:30 , Processed in 0.029822 second(s), 21 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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