设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑
    7 w  `" }% r7 p. T
    6 o5 V6 g3 l3 Q. L0 V解释的不错- |2 v: `/ g% I' d0 `

    ! w% H* m: }4 i# ?/ ]1 v递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。) ^! w% C/ f! V( w* R$ U

    + `8 l7 D/ G" U3 K8 h7 t) @ 关键要素! a2 M) w! C6 p; `
    1. **基线条件(Base Case)**- K  M. J) n0 M9 W" }3 C
       - 递归终止的条件,防止无限循环
    : m2 I5 f) q1 ^* G   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1/ e8 t8 D* _( b# N4 l: r8 g0 I' `
    / |2 w; h, u/ y* S+ b7 J
    2. **递归条件(Recursive Case)**
    ' K8 V; f  c; F: P# l6 g   - 将原问题分解为更小的子问题- ~  B- w4 O5 i  O
       - 例如:n! = n × (n-1)!
    % R# v# {/ H' }1 a4 G% E
    2 d$ C! o! j% X4 f  H9 Y 经典示例:计算阶乘( J( f& x8 \. ^; o0 _
    python
    0 z, E$ y( A  C% x4 @) U7 ?0 {def factorial(n):- p, _* p) F) t. }( u
        if n == 0:        # 基线条件
    / o# `6 D% h# Z$ @8 o1 M6 I& Z        return 1' |2 c) K. h9 F9 D7 {
        else:             # 递归条件2 I0 K( P* f- J9 Q5 ~
            return n * factorial(n-1)
    $ k: m! t7 }* d: n. x% r/ I5 I执行过程(以计算 3! 为例):
    + R" {+ x8 ^* J' x9 e8 T* J7 N4 mfactorial(3)7 Q2 x+ D8 E  a7 @( V8 G
    3 * factorial(2), q2 }! R* k- U& @; p
    3 * (2 * factorial(1))1 E( l  v* ~/ N7 j; O6 t( Y' e
    3 * (2 * (1 * factorial(0)))
    : a0 d; U3 R% c' a3 * (2 * (1 * 1)) = 6
    5 H( J6 j0 H2 N5 V& e. {" ^. k
    递归思维要点
    7 s7 M( ]0 u6 @  u5 W0 m* T1. **信任递归**:假设子问题已经解决,专注当前层逻辑( A* F6 D( F& W- J) f
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间), k3 v6 a# @) n. ^
    3. **递推过程**:不断向下分解问题(递)
    5 {8 }9 v. x; Q: `# z4. **回溯过程**:组合子问题结果返回(归)
    4 b6 }- H6 |* j4 ^$ o* m9 z
    4 S' r( K' N. a+ @6 Q( X4 Y) Y) J6 H 注意事项
    - p, V# n" |8 H必须要有终止条件
    ) I* G- M4 L7 T9 X递归深度过大可能导致栈溢出(Python默认递归深度约1000层)9 ^' p! s$ |/ D- l1 Y9 q
    某些问题用递归更直观(如树遍历),但效率可能不如迭代
    ( Y: C0 Y$ A" j1 _" Z- e尾递归优化可以提升效率(但Python不支持)
    , M6 r" r5 c3 e3 l; {( \2 W, f( l) J9 u$ D* |( Y
    递归 vs 迭代0 s( S- i6 m6 m' A# C, S, \  F
    |          | 递归                          | 迭代               |
    / T. I9 j( ~( R% S* ~' _  E|----------|-----------------------------|------------------|
    * q1 P9 s* O4 }% w, u9 k3 @. V| 实现方式    | 函数自调用                        | 循环结构            |
    2 x" J% v. J, c& h" H| 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |
    % T3 b- h% I7 R# g: a| 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |5 s: }, m3 P5 @9 S- s( g
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    8 p; V  F' D. G0 T, ]1 h. q' X( ]6 C0 o4 A0 T
    经典递归应用场景
    " _* J+ `8 Y7 \* R0 d7 V1. 文件系统遍历(目录树结构)' @% g$ B$ P$ ?0 {% D
    2. 快速排序/归并排序算法
    ; g- m0 I$ k( e6 {- `/ ^8 E( y6 ^3. 汉诺塔问题
    - ]" c! S$ H$ x2 p1 i. b4 q# k4. 二叉树遍历(前序/中序/后序)
    & z1 q, C$ L6 z0 C+ u+ v5. 生成所有可能的组合(回溯算法)
    ( R/ W" W3 c, H7 B. p8 A( S6 b" s8 N0 B/ |0 R
    试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

  • TA的每日心情
    擦汗
    3 小时前
  • 签到天数: 3193 天

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    " K* G7 I/ }) o8 Z1 u8 @我推理机的核心算法应该是二叉树遍历的变种。
    0 X$ e) q- H" a: X# f) |# t  y+ L- 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 ]6 j% w/ n& R4 P+ l: ~' f& oKey Idea of Recursion. {+ t# a& D4 {) |" I6 D! r# L

    9 Z) b3 x, L+ u* o( p% u8 H7 L4 \A recursive function solves a problem by:
    ) L7 Z2 e% \. }6 R1 r0 m  E( G7 J1 C# ]
        Breaking the problem into smaller instances of the same problem.
    4 Q: h3 @2 A' Y7 v3 v2 u" f) m& \4 r- ~& S
        Solving the smallest instance directly (base case).
    ' V! m3 G( N6 r* F7 M7 i2 [$ S3 H( N% ]1 k- j+ P* ], T
        Combining the results of smaller instances to solve the larger problem.; x& X& U" I$ O, v; z4 [

    . }) }4 `/ W2 U( F( |/ }4 Y  A1 aComponents of a Recursive Function
    - X1 U1 l4 s8 k3 H6 e% ^9 [( |: Y6 y  m
        Base Case:; K) o: V( Q  i

    - U) U7 G: m7 U0 Z0 C3 E* ^        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    & h: x/ `$ _6 u) ^
    4 H  f4 v2 s+ n9 Y/ {; X        It acts as the stopping condition to prevent infinite recursion." U/ s' O" b7 B( h' g5 l. b

    - ]  w- A# C& z: V9 B: b        Example: In calculating the factorial of a number, the base case is factorial(0) = 1.5 ^. P4 ~0 A. k

    + T' h  e1 p, n    Recursive Case:
    2 z$ C! ]# ?* D! b2 l# Z
    - J5 s. L3 n, n  S/ v7 l; c( I        This is where the function calls itself with a smaller or simpler version of the problem.
    * n( g/ M/ O" e% n0 A
    * h2 C! ^6 L6 i9 m  t        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).
    / k( P$ |9 M( S. _! M& c/ S! A2 U- V3 `
    # C4 E9 A, V& x+ P# I0 D8 MExample: Factorial Calculation/ e: i, O$ ?  n! ]/ P4 t

    " J/ z& X0 Y# k  ~2 ?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:; c7 q' _: n# v" X
    0 k, @- h7 j  C5 b
        Base case: 0! = 1
    5 G2 `) U  }( A3 O
      L; h, n4 |; Z: C  F    Recursive case: n! = n * (n-1)!
      |6 h3 d0 R4 ~5 L' i6 P7 V; }
    + ]/ |& f* [% G$ GHere’s how it looks in code (Python):
    " O* P$ Q, u5 Z% {+ {3 I6 Kpython
    ( L$ J$ E5 T6 I) x* V
    # ?  U: [* c- E5 W9 ~# x4 L* P/ x$ H3 Z) Z' a" C
    def factorial(n):$ [. P& s3 b8 {7 y
        # Base case/ ^3 E9 z* ~( c/ a; @/ l$ X
        if n == 0:
    ' u2 \; [9 [6 b$ j+ M3 Z, H% ]" d        return 1
    7 x% P$ |- @9 y+ |    # Recursive case
    7 f$ }' g' o4 |4 F4 }1 ]    else:! h7 h' f, W& O9 [0 j8 `
            return n * factorial(n - 1)9 G5 Y4 N. Y. v4 j( D$ J5 B
    4 x1 n. O% b% ?: P
    # Example usage
    4 @4 h4 A3 e5 [8 Bprint(factorial(5))  # Output: 1209 i; ^- m5 o$ i- Z1 }8 i: J
    7 R" S8 a6 ^% b! q' p0 v
    How Recursion Works
    " `5 C5 x. F7 ]. Y+ M% W7 U: j* A& h4 T2 \
        The function keeps calling itself with smaller inputs until it reaches the base case.. V! q' G4 t+ E2 {+ O

    0 o- g4 V( e) x* w  e: s! |; N    Once the base case is reached, the function starts returning values back up the call stack.
    % ^3 z  Z6 z9 @$ R: d/ {
    2 r6 ~0 ~1 ^1 G6 N    These returned values are combined to produce the final result.
    5 E) m) |1 T. t0 h: e, R2 B* C! D$ _4 o2 r" W9 r1 V: s
    For factorial(5):. U, b# i4 L" c( ~0 z7 ]

    9 A: _4 c# |; J( P
    + @3 ~. c. I8 pfactorial(5) = 5 * factorial(4)6 |1 Y* H& h1 b& ^* w, a
    factorial(4) = 4 * factorial(3): T# _% Q. O3 K$ q, M. h
    factorial(3) = 3 * factorial(2)2 ?( m5 r. W  ^6 E6 A
    factorial(2) = 2 * factorial(1)7 R. H2 Z2 |) v
    factorial(1) = 1 * factorial(0); A& R  Y# c2 O7 M8 G# q2 T
    factorial(0) = 1  # Base case! \  I6 c1 W; ^7 h2 N( [+ Z
    " @9 d( |1 T& x1 M5 }2 P
    Then, the results are combined:, p3 D2 d$ [5 D3 {

    4 a$ y% T/ Q. |
    6 a6 {" E3 V& }7 K' @! s8 yfactorial(1) = 1 * 1 = 19 \, c1 i5 E% `; P; C7 _; W
    factorial(2) = 2 * 1 = 2
    + i% D( {/ W: [$ n4 M& Dfactorial(3) = 3 * 2 = 6; W6 J2 @* N  }6 l, f4 a! c
    factorial(4) = 4 * 6 = 245 @2 q/ X$ q8 t, b& L/ q
    factorial(5) = 5 * 24 = 120
    ; m3 i/ W+ W; ?. g9 ?8 @7 [3 |1 }! Q+ q% [+ y
    Advantages of Recursion9 \0 u6 J) T: @0 Y

    & \+ z( O% @& c    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).
    5 E* l6 d1 {* Y& _6 _1 b' h8 l8 g% W+ x1 ]9 `6 U, T/ B3 D5 N
        Readability: Recursive code can be more readable and concise compared to iterative solutions.
    * L( t6 t+ m& ^/ p) M. E2 Z3 f" c: n0 O& r
    Disadvantages of Recursion* [& I! p! H! }: I, g( {: X0 t! R

    + U& u: h, C) a    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.) R/ z* j+ A* h. Y9 E' {9 n

    7 g3 N& V* `. X/ l4 ~    Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).6 b9 o% n- ^# G' j* ?

    $ e% N" t4 g/ e6 l7 z2 r& ]6 }When to Use Recursion
    # R4 g) ?2 d2 H6 O$ I, Q! d
    5 ?  c7 b  e% B4 _" J8 E! v  S    Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).8 j9 T" W' L  _9 e. s. W) y, U

    # E; J  r6 W& R# y  i3 z    Problems with a clear base case and recursive case.* S: E, v( G7 B# h! u9 V( T* h

    : V. K/ z7 T+ l3 I' OExample: Fibonacci Sequence# a2 x; I* T. B& o( B  Y. {' m

    1 F2 v3 B& d' YThe Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:
    : E& r4 M' ?0 S  e8 T; f; ^0 v! U# E+ L  n/ W
        Base case: fib(0) = 0, fib(1) = 1$ L6 M7 U/ ~( j* j% K* d$ s4 x
    8 B1 h5 p+ b* d! V: m7 I4 w
        Recursive case: fib(n) = fib(n-1) + fib(n-2)" N- N" Q# B+ J' a& i0 j+ H
    ) W$ N( ?- U. @# ]' w# C0 h8 \
    python
    - j  Y2 D' ?5 s* v
    % @1 V) O! N& l: ~3 B$ m/ a. a( N- D7 }4 u) S; F0 r
    def fibonacci(n):% H1 D% H# R; e0 h8 U
        # Base cases
    ; o  w5 B- K% e. F/ n) `2 R1 A2 I% g6 c    if n == 0:& p& q- Z) Q: e  V
            return 00 `- B7 u1 B' A0 T+ x6 E
        elif n == 1:& @7 K8 N9 ~0 a6 }3 K' D+ e
            return 1; B4 k  i% e  n: l3 {2 A
        # Recursive case0 Z  u/ S* M# j6 `% a" T, }( X
        else:8 a$ \! g' S9 U6 i$ S# r, a& V3 r  G. D
            return fibonacci(n - 1) + fibonacci(n - 2)
    & D3 i" S) q- u5 o4 G) @: G- C
    # Y* X7 @8 i8 X/ r# Example usage
      G4 K. z/ Z. E7 ?" bprint(fibonacci(6))  # Output: 8
    # [  T" y/ E+ T5 O# h. Q) M1 M- `
    Tail Recursion# E- Y! ^9 y: R) r- n: e

    3 a2 {# q1 D% g, R2 T& TTail 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).
    ( u, r: a% |3 Z
    1 O' k0 p7 ]9 l0 h% IIn 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-6 10:41 , Processed in 0.057357 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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