设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 5 o5 Q3 p# V, K0 b- T7 _

    2 @/ U$ c  P- e0 G解释的不错
    ; g' v' ?( {* ?5 F- n" b$ y5 {5 l' G) R+ G
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。. N, g- [3 W+ _0 E) G  z
    7 V: B( P# U' ?7 g3 A% L- h" K
    关键要素
    7 c8 z: z- K4 f1 L' @6 W2 b1. **基线条件(Base Case)**
    6 m+ C) Q$ |; j) T   - 递归终止的条件,防止无限循环& _: A- V8 G' Y" u  M# B) p& }# L
       - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1+ C4 o, [/ v$ n

    - k2 |- o0 X8 k. Z  `$ @6 R2. **递归条件(Recursive Case)**) [' [( ]6 B( [
       - 将原问题分解为更小的子问题$ p7 w2 F% R) T8 F
       - 例如:n! = n × (n-1)!5 @- ?6 O- Z  d8 O& r6 y3 o4 ~

    ! w  R! {% x2 Z5 p 经典示例:计算阶乘
    5 T, d6 s3 a7 Jpython4 G: z, g- G  ]" @" [* V
    def factorial(n):) j( J- o4 w5 K4 G
        if n == 0:        # 基线条件
    ; r; T3 j. b3 T% y0 o: e- o        return 15 T& a% g8 |' T1 W
        else:             # 递归条件1 k  k& ~; q1 ?9 Y1 H# K& K
            return n * factorial(n-1)( [% k' b' T+ T3 O
    执行过程(以计算 3! 为例):9 |9 k, z/ T2 N2 b
    factorial(3)# h1 B2 B- V# @' k) I. |
    3 * factorial(2)) C; ^. H0 q& T- r) I0 t9 ]
    3 * (2 * factorial(1))
    ( h- h% O3 B1 ^3 * (2 * (1 * factorial(0))); v8 x5 }: O: D2 F
    3 * (2 * (1 * 1)) = 6
    2 k  k; f% I% v1 y6 |+ v
    # U# k" i+ B) K/ P; f. K 递归思维要点! W2 |+ \% q' A
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑
    ; P; S! q5 A+ B2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    0 ~9 v3 k' [. H3. **递推过程**:不断向下分解问题(递)
    . v2 I( E# [: s9 U, j7 H6 G% }+ I4. **回溯过程**:组合子问题结果返回(归)
    ' {) Y% d& E) g$ s# Z
    5 J9 L* h! W' ~, h- h 注意事项
    ) ^  `8 q( E" P必须要有终止条件- m0 |/ B4 C0 t0 q/ Y0 P
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)" j4 J: s  k7 U8 ^  @
    某些问题用递归更直观(如树遍历),但效率可能不如迭代$ S% a$ ]; e/ a" F8 @- A
    尾递归优化可以提升效率(但Python不支持)5 _' K& C, N) C9 w( A# d  j
    ' c. O, v" o( L# f! B* G
    递归 vs 迭代
    8 M5 z: B- e: A4 v|          | 递归                          | 迭代               |
    $ U# x$ I% t' A* `  R/ Q! p|----------|-----------------------------|------------------|
    ; [- D6 G+ U, I0 m- e: p| 实现方式    | 函数自调用                        | 循环结构            |
    5 H8 M& ?; o& k  g9 d  E| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    / l) R- a( Z& J) H' x| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |
    # t, @! W  f5 ^| 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    $ @& T3 A0 M7 b' n
    $ k6 I! Q; _4 C0 H2 t' ^  R" Q 经典递归应用场景. P" H! c+ X7 g3 i2 q
    1. 文件系统遍历(目录树结构)
    ! d$ R; E; V& t( e2. 快速排序/归并排序算法
    - h8 M9 M0 W, c! L4 N5 h3. 汉诺塔问题
    / ?6 ~  }5 Z# W6 o4. 二叉树遍历(前序/中序/后序)+ e3 N+ v) A2 S* c! G, u, G2 P- I! Y
    5. 生成所有可能的组合(回溯算法)
    0 N8 H' `5 e% z3 Y, g+ ]6 b6 q% L
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

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

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    1 m) T9 V1 t5 b7 C+ M我推理机的核心算法应该是二叉树遍历的变种。; A* ^9 [* H- H9 @+ D3 y- }
    另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:  b8 P1 F0 ?* D3 Q5 a/ ]
    Key Idea of Recursion8 s9 h  r2 w2 Y( w( u* y

    $ ]7 v6 ?4 i2 D' m8 O1 gA recursive function solves a problem by:6 e/ }$ i1 A2 J9 B: E; v* ~- l1 v3 c4 b

    ) o, T8 S6 H2 d! h: Q- O    Breaking the problem into smaller instances of the same problem.$ Q. @( c" K! R

    1 M# O1 E' `4 u* [    Solving the smallest instance directly (base case).8 \6 |! z% s5 H& |/ f- o

    - t& _: E# j$ q& [    Combining the results of smaller instances to solve the larger problem.# o8 P8 ^! m5 Q0 X$ @+ V' O
      B' Y" J6 N8 ^$ l
    Components of a Recursive Function4 n; v) k* |1 k. G* E
    5 e, R9 H9 R6 C1 I3 R, M0 I
        Base Case:5 c* ~: a5 [$ N/ x" l

    8 C& Y& P  p1 ^: M. W        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    1 T' C- m/ k2 @+ q
    7 a9 s6 U, \6 a$ P1 ?: S        It acts as the stopping condition to prevent infinite recursion.; T8 P2 q, I' j
    ( L2 l7 g* y( O. t; p  U
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.' u) {" T1 i! N  p- J7 I8 W( }
    # L7 U/ j' P- g/ B  v9 N: a2 k0 y6 F) m
        Recursive Case:
    % ?; x  C; Q0 J5 J; |6 E! h3 W& G# B
            This is where the function calls itself with a smaller or simpler version of the problem.! N! B' v1 o3 o& Q$ V

    , L* m% H3 V4 |" R- P        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).: Q8 S2 H. O* \5 A5 ]1 Q

    , t$ B# P( c, I' F4 s5 ]* bExample: Factorial Calculation9 u1 g1 `+ T! P3 C  P9 ]& ]3 Y

    0 y, I9 h! I* P4 B7 f) UThe 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:2 s1 ]3 e8 l* C. I8 d7 y9 M& i
    ( Z& _5 y; ^- U; I) _' C6 \
        Base case: 0! = 1
    2 P" c' V5 q! U/ E  Q1 d0 i0 ?2 {4 ]7 D' y/ h, D1 K. ^
        Recursive case: n! = n * (n-1)!
    ; A1 c. p! a( R+ [4 F3 ]* g; O
    ) `5 p& n6 \; j. c: p0 _Here’s how it looks in code (Python):
      e( f* N4 n7 W! bpython
    ; o; V1 x3 Z' w  d- O; N6 Y0 E, j8 d3 c

    , J( i7 [& v' o, }def factorial(n):( [( e* p- G" C' D
        # Base case
    * Y$ ^1 _! A# {) X    if n == 0:
    7 E6 M( P1 v0 T  ~        return 1! i: M+ b& O* B0 Z  s
        # Recursive case
    % A& h# x$ P9 y3 Z( ~4 q! v    else:- n3 h9 ~+ J& _8 E2 c& u$ G; Z
            return n * factorial(n - 1)
    2 k  M$ E9 H( g" z  n% ]) {! R# c) \5 \( `5 g
    # Example usage. N3 q7 f" r$ g  j7 H
    print(factorial(5))  # Output: 120% q7 @5 @1 m% [

    , @" `9 n7 B9 y% w! {How Recursion Works
    ) A' Z& ~; J2 r
    & c+ ?$ J0 v, `4 L7 D$ g# o3 E    The function keeps calling itself with smaller inputs until it reaches the base case.
    % n7 q# }) {" b2 b, u4 h( \1 K
    . }. G% N0 i# d/ X    Once the base case is reached, the function starts returning values back up the call stack.
    % L, }; o) ^0 N3 A' x! a
    5 o# y) E4 q; h  U    These returned values are combined to produce the final result.* a; b" m/ i* n6 T. W7 P, A% _" Y

    , A9 @6 Z. I0 ~# GFor factorial(5):
    " `0 H+ U0 ]. u% u. ]* g; r
    ( f$ P. m$ W/ i  j$ E
    . V; y/ D# x+ ?0 ufactorial(5) = 5 * factorial(4)
      G3 i2 i" ]) V' ?0 h$ H8 k2 wfactorial(4) = 4 * factorial(3)* G7 X. Q2 i, ~) F% `) \
    factorial(3) = 3 * factorial(2)
    # _$ v6 S: P3 x% ?  _  f$ Sfactorial(2) = 2 * factorial(1); Y& H0 y3 d: M4 z
    factorial(1) = 1 * factorial(0)
    * P' W& ]8 Y% ^; m, P1 u, K2 X/ F, Hfactorial(0) = 1  # Base case
    & k, a0 [; ~4 v+ \, R% O. C
    6 q8 D6 N/ k9 v  N" F3 ]Then, the results are combined:
    1 m7 P# k' c" q0 \1 R  ~
    7 ?0 I( T) g( D' K+ w4 _& M+ Q, F6 `& j) ]
    factorial(1) = 1 * 1 = 11 W" x  t5 `+ N! D! z8 c& I
    factorial(2) = 2 * 1 = 2
    7 `% l- x3 M5 c/ B$ a  K$ x# A- @factorial(3) = 3 * 2 = 67 q- g1 _) G- `2 X' k
    factorial(4) = 4 * 6 = 24
    & s. o3 e" `- Q' yfactorial(5) = 5 * 24 = 120
      `) t2 j3 q, y' v, F% e5 V7 q$ a! u
    ; V) x5 P5 z- fAdvantages of Recursion3 p  b% B, P. w* v; ~

    ; K4 t5 @# `+ J) r, Q7 @    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).
    . b% z. P2 i; E. P+ }% ?( C! Q: F) O  W* Q& T- R/ Q
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    % M. a  T" d& t* k/ {# N! g$ c* h4 }
    Disadvantages of Recursion5 Y& q7 i4 c5 U! I, ]2 H3 v, V
    / w- l# r6 H. X5 N
        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.
    ; w& W1 T/ B  F7 s/ {4 Q* D1 F) e2 y8 K) j4 q
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).. M. O4 L( Y7 Y1 S4 g
    ( [" z5 V, u  k0 |$ A! J/ Y
    When to Use Recursion
    / Z* v5 L3 {' N' h7 N- p( k
    , ^" y% q; ~8 r2 S( b5 q' E# z    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).
    / y8 A% p9 U0 r) a* N: O4 V/ @' w
    ; \- X" T; |# f- t$ F# \4 ~0 ~" F1 k    Problems with a clear base case and recursive case.
    # T  C9 `9 {( n0 P  z2 z
    % ]" b4 @' _2 A: d, \( [Example: Fibonacci Sequence2 `3 W" r  ~2 M/ ?

    % Q! E" o& v- [# F( j  QThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:% F- b: \  h3 T
    ! R4 `$ c, \6 m1 c* l
        Base case: fib(0) = 0, fib(1) = 1" P" q5 U( f  \& T% o
    5 z( Z  S( V. h
        Recursive case: fib(n) = fib(n-1) + fib(n-2)7 M! s2 {3 c  g  X6 g9 P3 ^# u
    # Q, t# ?' `4 {9 c8 [( [: a
    python- j. D& N, q- ^& t+ Q! a" ^
    6 t. g; t4 L* ~4 A( A- g! g
    ! `; e4 S9 m# n& ^
    def fibonacci(n):
    " k& f' T8 R+ Y3 |4 x% s0 z    # Base cases- v* s- B9 c( q' l0 W, _( }
        if n == 0:
    1 {$ }( o# i: Y. P, \. U/ B3 L        return 0
    5 R, P5 G7 W5 Z3 O    elif n == 1:) S/ a  f$ v: [$ F
            return 1
    - U( z' l  L& t3 M- W* y( o: \# G    # Recursive case
    / o3 g/ X+ ^5 r) J    else:
    , U: H  z( S' \( t7 L0 E) y        return fibonacci(n - 1) + fibonacci(n - 2)& }: g0 U4 V8 S% z8 K# S
    , D3 |4 J! F3 r
    # Example usage4 D" Z0 R1 K! h% ~" s5 r
    print(fibonacci(6))  # Output: 80 C- W' z2 x0 n5 O4 V

    ( I. W; ]+ K5 U8 z5 ETail Recursion
    3 Q2 o+ ^: w% Z4 [) o, T& \' `3 F3 q3 x! E$ s
    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).
    1 L7 U: y% i6 F0 t* @
    " v  S0 C8 t$ l* jIn 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-9-20 12:13 , Processed in 0.056429 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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