设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    5 k0 f! ~0 q- e4 s
    + q4 a, x1 q9 z9 p2 v解释的不错
    ! I7 c( w, ~. G7 \  ?" R! r8 s  @1 Z* ]$ P% l  r% b
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。$ h3 I! A; _# h' |
    6 e# e8 {5 I# A5 Z# Z8 B6 s% a. e
    关键要素2 q" Y+ r6 O  b1 o+ Q1 q* ^) \
    1. **基线条件(Base Case)**
    + A  |: r- R, ~2 D; z" C4 s   - 递归终止的条件,防止无限循环
    2 i. Z1 [# Q) ]5 A/ n% v' _  g   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 11 Q3 k- k. J: E- {

    . R# p. n" U, N1 E9 f2. **递归条件(Recursive Case)**' M5 t& C! L$ Y! d
       - 将原问题分解为更小的子问题
      l5 \4 b- l/ @# h   - 例如:n! = n × (n-1)!
    9 ^7 {* t0 g# E  x+ f3 t1 z/ F* E0 D8 D* z( ]( R+ q; }
    经典示例:计算阶乘- V$ r0 E; _; Q5 i1 q7 H
    python/ S! _" Q2 p. B8 n
    def factorial(n):* K$ V5 d+ e; h
        if n == 0:        # 基线条件- r& x" O. @1 a/ U
            return 13 u! @4 Q5 g1 u, l. m7 Y" x
        else:             # 递归条件
    ( U; Z. J0 z1 K- ]1 @1 j        return n * factorial(n-1)! j) r7 z$ z& k7 j2 ^5 W
    执行过程(以计算 3! 为例):
    ) Z  i. F4 i6 Z0 R3 C- yfactorial(3)$ G4 a- E; _" ?1 @# w; K. P
    3 * factorial(2)
    ' F8 z" o% u/ _( k0 ]3 * (2 * factorial(1))* _) U! G& V: x" d# G, b
    3 * (2 * (1 * factorial(0)))
    - x8 ]5 M) ?8 C+ A3 * (2 * (1 * 1)) = 6" E8 a) Z0 Q1 ^# A. V
    4 M, E& s8 a. N) f& N
    递归思维要点, o2 j! v( Z- J% f( h
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    3 {/ n: s4 K8 f+ S2 ^7 A6 f2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    ! T( O2 I* s5 f+ |9 a3. **递推过程**:不断向下分解问题(递)3 z3 ?( p9 \( Z% v) C+ V8 d4 R  o8 _
    4. **回溯过程**:组合子问题结果返回(归)
    4 y5 O5 r& x* Z% z( o; K
    ) W+ h& j2 i& R 注意事项
    ; B- }* }# D7 u; i, R% O必须要有终止条件. z2 C# ~4 |: d. ]  f: f) q+ o" ]
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)+ I# i; e! m8 K# |9 [& I, S
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    ) ^$ b- ?& _' z6 y' @$ z尾递归优化可以提升效率(但Python不支持)
    . T9 D$ Y4 A* E( ^9 Z2 u7 S. B, K) p! O8 A
    递归 vs 迭代. [7 E1 `) N) U* M/ Z$ @8 f8 R- W" a
    |          | 递归                          | 迭代               |
    ( g; S( D4 _) z! A& t# p- \' o|----------|-----------------------------|------------------|
    9 H# K2 x" T4 Y1 E  T6 L| 实现方式    | 函数自调用                        | 循环结构            |+ R0 A+ m& Y- O
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |8 n- z  ^" O- x# e! N) v8 M
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |) h% y. }' T4 o- B9 p
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |+ [/ U% D' E& m- w) @
    1 m9 h1 o* a- N! G
    经典递归应用场景
    % a6 s. y) r- M  ^. G, w" q1. 文件系统遍历(目录树结构)
    ( y" J+ k9 Q6 B& r1 g2. 快速排序/归并排序算法
    % E/ T# H  S: X$ \" T3. 汉诺塔问题
    # ?, M8 F% b* \0 t, D4. 二叉树遍历(前序/中序/后序)
    + G$ b9 g0 K5 S, y5. 生成所有可能的组合(回溯算法). O( m' o5 d. Q! ]# m

    5 ]2 N* G+ |; ~' y* c; N) U试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    郁闷
    前天 07:20
  • 签到天数: 3208 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,. W& j2 i5 r5 B) T8 c* R2 M; Y# L
    我推理机的核心算法应该是二叉树遍历的变种。
      [" k" I3 _' @. r另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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 L. [/ n6 [4 r
    Key Idea of Recursion
    & o1 c. i0 Y7 B: k# x, r: j% v; x# j0 @
    A recursive function solves a problem by:# G. r/ b+ p: L& E% T) q

    $ \8 C! o9 Z  p; D4 e. T- V, M5 w9 D( ^    Breaking the problem into smaller instances of the same problem.
    ! d& o! K" s7 g9 n" J; g, @( N/ F4 ?: N& J& n3 y
        Solving the smallest instance directly (base case).3 p* G) o0 y) X
    + d" U) p# K& x5 X( ^
        Combining the results of smaller instances to solve the larger problem.
    ; O5 \$ k7 q7 U, T6 b' i( R7 ~; @/ e" t* _* S, N. ?# R! A( ~
    Components of a Recursive Function+ U5 V, G% }& n5 U4 P! T( C
    , ?0 Q* l: U$ Y) y1 A: h( E
        Base Case:) M# ^3 l4 t  b# q' r

      G8 v- ^5 W% s, H8 \        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    0 t; h7 c. S8 w- }7 O2 F5 m8 d$ s# P/ t8 J' N0 S7 p; \
            It acts as the stopping condition to prevent infinite recursion.
    7 k/ b, V5 m7 I3 J% v4 A: X) g, e7 H' C$ C) Y2 X
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.- i0 b4 n. @( R" E8 p0 p
      O- U3 k4 W1 }/ N# @8 c+ f! R
        Recursive Case:0 N. E+ D" {7 [! D+ Z

    4 F1 c% l2 `, i- J1 {+ z6 p        This is where the function calls itself with a smaller or simpler version of the problem.; y( |8 p% s3 ~1 I8 m1 g) m' A
    # D7 D% a. k5 }0 `/ \- P; b" G/ s
            Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1)./ u9 ^9 U# |: V  q! X: ?0 c
    ) b3 v/ k$ {* l* H9 {
    Example: Factorial Calculation7 E1 S0 i& q. F3 k# I  B
    2 x7 _" v3 A1 _$ B+ j
    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:0 e) M) m! e( r* U' I2 g8 E
    ! }4 {0 }+ S3 @
        Base case: 0! = 1
    2 b9 S+ Y8 @, e( {% v( U6 _4 q; k  o  ?8 D; P
        Recursive case: n! = n * (n-1)!
    " ~3 I: @1 i% G: d$ k9 X$ {3 N* F- G8 _0 ^& N- U$ D7 Q
    Here’s how it looks in code (Python):" B9 \- J; J# u" F, {
    python9 _* k7 O/ Y0 R! v+ f! r1 S$ \4 t
    ( j: j, m0 n4 q8 M) n

    / I$ g! S( C0 H9 T; C: @5 hdef factorial(n):
    ; P/ ~" i) d3 G5 r$ u' T& \    # Base case
    3 h; t* f* k- P    if n == 0:
    # T/ D# N: r9 k" c2 ^        return 1' }* D3 n* n# ^" X. P. j, q
        # Recursive case
    ( A/ C4 M0 q! R7 q$ s  C0 A    else:; p. s* d9 R( ?/ f, N
            return n * factorial(n - 1)
      E; }1 A) r# h5 {, D, G: d2 n, O: {5 G8 c/ y% O9 l/ [: |
    # Example usage
      t6 H( p$ W9 N% `print(factorial(5))  # Output: 120
    ' g0 f& r1 d( b* p
      |: R2 Z0 ]! Z' aHow Recursion Works
    ' d; |4 f( \9 W  Q0 S# Q+ }. U$ d. N* e9 T1 t0 j, n1 f/ T
        The function keeps calling itself with smaller inputs until it reaches the base case.8 k9 K( U+ B+ P% H9 U. O8 F" R% y
    2 j8 {% s: x# O/ {2 S
        Once the base case is reached, the function starts returning values back up the call stack.8 E9 z4 r+ c5 l! o- x1 v. c  U

    8 i6 c' C' r# {" _( C& `4 Z    These returned values are combined to produce the final result.
    ) Q! Z6 Q- D8 C3 a  w5 ]2 ?; x% W7 b' K2 s( y: c5 `
    For factorial(5):
    , d; V  {4 b7 _2 h8 s1 \) c+ J' o0 d' D4 D" b7 N" @
    , E2 I  s$ e- L: W! }
    factorial(5) = 5 * factorial(4)
    ; U- Y" x6 l& }* O3 u+ tfactorial(4) = 4 * factorial(3)
    8 m: l. r& s6 S% m3 K0 m/ Xfactorial(3) = 3 * factorial(2)9 ]& M. ~( L! y# T+ O/ e
    factorial(2) = 2 * factorial(1)
    # K2 y8 O4 h5 L: F) m$ Afactorial(1) = 1 * factorial(0)
    # V: D7 t. x6 F' w: z8 U0 m: X$ Y$ Rfactorial(0) = 1  # Base case3 o" k5 c+ x1 X% \. n

    0 S) j7 z" W+ I1 lThen, the results are combined:
    " O6 ?% f& ], P$ N2 `% N1 z5 l) \
    , i1 [$ a) ^0 F: R% F
    factorial(1) = 1 * 1 = 10 z6 H, H! ^7 ~1 Z$ G
    factorial(2) = 2 * 1 = 2
    ' [4 d* ?0 C- Z+ v* _) }% Bfactorial(3) = 3 * 2 = 61 b% B1 m( ^, v9 t7 A
    factorial(4) = 4 * 6 = 24, e( `8 }" o" s( z' z5 N" @
    factorial(5) = 5 * 24 = 120) [% j. K! ~. h! M

    ; i9 C/ c- y4 Z: n. k2 y; g2 S; w. mAdvantages of Recursion, Z! _8 w  @# r! c1 R+ y1 r* ~
    % }" r8 ^$ D+ D
        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).
    8 _6 A& R$ c# S* J1 g5 s3 L+ O5 F) y2 c% Y. i' R! `0 j3 S
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    9 m: r/ R* V- C# M8 q: E2 b, O6 l$ ^8 \; k5 s% j7 m
    Disadvantages of Recursion
    ' ?/ ~6 h3 D3 U/ H
    8 O* N, w6 E9 [    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.1 E+ ]# h, \4 U8 M

    # W7 b7 U0 [9 q4 E* E& K& y$ \! j    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).
    * _1 R3 k2 X. i) W8 M& Z6 N0 K( w4 h8 n) G3 O
    When to Use Recursion2 @3 ~: u8 c  T$ S" Y% P' Y
    - o8 f5 T4 C6 I
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).) q4 `( ]- P' f) g: j# `( ~
    4 S& d" a' n: `2 G) O1 W
        Problems with a clear base case and recursive case./ [" [! a/ H8 e  a3 m5 x
    - d: P. r1 f1 e
    Example: Fibonacci Sequence' }7 b* y; E3 `; b, g0 S
    0 A1 t; V" G8 }% a; T
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    ; v5 U% Z) p4 p3 W
    " @# N$ v3 ~/ @1 h. o3 T    Base case: fib(0) = 0, fib(1) = 1
    6 P9 s; K) p: {4 Y2 |$ D1 U; M, f- J
        Recursive case: fib(n) = fib(n-1) + fib(n-2)
    * S1 [  b3 T2 d; [% B6 W) {
    ; e. d# B- ~! o; @5 Spython/ |3 @* v& o- a, \6 P

    0 [3 I9 K' v. q6 a) \  r- `) B+ A
    2 U- @2 @1 G0 L* K3 H0 `def fibonacci(n):
    ; f$ d- \$ E7 s    # Base cases9 R* Z) b2 ]$ `, d" A5 F* n* P, V
        if n == 0:
    : P+ B& X. ~9 B( N- V, l* m        return 0
    4 h) q, M3 N9 M) Q  D* x4 s; |$ R    elif n == 1:1 i3 \) W) I/ R( E; w7 S. b6 r
            return 1
    ! q# u! {* ?/ B' Y! o    # Recursive case
    $ G% J* g" ~% R+ w" R( {    else:
    # X9 U) I! Q" H: F        return fibonacci(n - 1) + fibonacci(n - 2): ^' B9 T% X, f, G; i$ k0 s+ n

    ; p4 }8 U4 L! o5 T# ~# Example usage
    * b4 c6 N5 ?6 Gprint(fibonacci(6))  # Output: 8; T, K+ N& i: u& R

    2 i( ^- F- [3 f, n0 `, U/ mTail Recursion
    " X# F% c/ n# l" Q& H+ O- C3 T) G2 e* 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).
    ) V1 P6 ]+ r- q" [: `' s3 Y: ?1 |4 ?- i: k. K8 C% D3 ^3 k. 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-3-25 22:36 , Processed in 0.062369 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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