设为首页收藏本站

爱吱声

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

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

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

    [LV.2]筑基

    跳转到指定楼层
    楼主
     楼主| 发表于 2025-1-29 14:16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 密银 于 2025-1-29 14:19 编辑 ' l! m3 X% m2 g

    ; W1 X5 A1 K) ]( }" ~3 h. R5 `# N解释的不错
    ' b% l* H: C& S/ G/ V1 S( A, Z) n1 |: v# l& ?
    递归是一种通过将问题分解为更小的同类子问题来解决问题的方法。它的核心思想是:**函数直接或间接地调用自身**,直到满足终止条件。
    8 p. Y/ Q5 X* W; O. L6 f3 h8 |% R) A' F& g
    关键要素
    " ~8 s5 D! i% W, H1. **基线条件(Base Case)**+ O' d1 [! g0 C& K( w% c& \
       - 递归终止的条件,防止无限循环
    " s' q% }9 C$ H2 j4 h* Y4 a# J5 i   - 例如:计算阶乘时 n == 0 或 n == 1 时返回 1! i' L8 M- }) I# |  P3 n  K

    ' H3 `! t& t6 Y) t2. **递归条件(Recursive Case)**# q4 |2 R7 P& L$ t. _+ D5 x
       - 将原问题分解为更小的子问题  v! k2 z8 O, h) R3 G! m& b
       - 例如:n! = n × (n-1)!9 C& I+ q9 ?# p, ^# g' t( h; `4 E

    / L  n5 n, d3 J7 p4 P 经典示例:计算阶乘3 P* M% _( _4 U4 r- [
    python) F8 U* n- f6 a9 U* `$ K* z
    def factorial(n):9 e# l) Z( _1 m4 m
        if n == 0:        # 基线条件
    ' v6 d/ y  X  \: \$ J- _5 ~: T        return 17 {. H0 ~# a' v1 p; m5 |/ S2 [
        else:             # 递归条件8 D/ `( M0 J8 @$ U- |, N7 g/ ^
            return n * factorial(n-1)$ z7 N: I6 q) H6 ~
    执行过程(以计算 3! 为例):
    ( D4 J4 B# K# E2 i6 e. i+ Gfactorial(3)
    5 w6 }9 {/ C, y$ b. A/ \3 * factorial(2)
    , h! X, X' |( p: y& X3 * (2 * factorial(1))1 u/ K1 O# I" t, Z. H3 P
    3 * (2 * (1 * factorial(0)))" O# q) o4 q  d$ V
    3 * (2 * (1 * 1)) = 6
    7 T% x' L3 a2 M7 e) ]
    ; d  [+ A. K8 G3 P3 V 递归思维要点2 Y$ w! ?4 r+ b
    1. **信任递归**:假设子问题已经解决,专注当前层逻辑9 _: }/ d, D# i; O
    2. **栈结构**:每次调用都会创建新的栈帧(内存空间)
    / o) _2 G) R" l/ @: w% G: T7 g5 K3. **递推过程**:不断向下分解问题(递)4 J4 L6 U3 M, `2 l. `
    4. **回溯过程**:组合子问题结果返回(归)
    $ B0 W( v* {4 {( `3 l# _
    & {( W! |/ t6 j- a, I6 f6 A 注意事项
    ) [9 [, L! j: K必须要有终止条件% h& h' Y5 L- @+ e2 l) ^8 A
    递归深度过大可能导致栈溢出(Python默认递归深度约1000层)
    % Z/ C' R) f; ]; L1 F某些问题用递归更直观(如树遍历),但效率可能不如迭代
    + J, ~, O  M- Z, E0 D8 R5 r* W尾递归优化可以提升效率(但Python不支持)
    ' E( M2 f0 C1 z: }2 ]$ e1 P
    * m, @. U: q1 q+ X+ Y5 X- n 递归 vs 迭代, t* I* c! _5 f% Z8 b" h% j
    |          | 递归                          | 迭代               |
    5 s+ t' Y+ J! s; s- O/ m7 `. m|----------|-----------------------------|------------------|0 F" I- `. s& {& o; x# K
    | 实现方式    | 函数自调用                        | 循环结构            |" i3 \% @1 C. S% Y" |0 l. N% [+ O, N
    | 内存消耗    | 需要维护调用栈(可能溢出)               | 通常更节省内存         |+ L. E6 p9 I4 j
    | 代码可读性  | 对符合递归思维的问题更直观                | 线性流程更直接         |6 s2 [6 v  N$ f3 H- Z7 z
    | 适用场景    | 树结构、分治算法、回溯问题等               | 简单重复操作          |
    * X0 |' ^1 N% V
    ! `* x. B' L" L4 Q 经典递归应用场景+ H( E# L4 a3 X; m
    1. 文件系统遍历(目录树结构). Q2 V- s* J% [3 M9 ^
    2. 快速排序/归并排序算法
    + E  a& P7 n" r% f  S" p  n3. 汉诺塔问题/ U+ o, F$ A: Y! J: s8 K) ?
    4. 二叉树遍历(前序/中序/后序)- x) K, V' C" D# c$ O" [9 `
    5. 生成所有可能的组合(回溯算法)
    / t4 b4 D; _( v1 }) x: }
    $ }: w! V4 b% k试着用递归思维想象:你站在一面镜子前,镜子里有无数个逐渐变小的你在照镜子,这就是递归的直观体现。但记住每个"分身"最终都要有结束的时刻,这就是基线条件的重要性。

    评分

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

    查看全部评分

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

    [LV.Master]无

    沙发
    发表于 2025-1-30 00:07:50 | 只看该作者
    挺好,递归思维要点与我能够回忆起来我当时写递归程序的思路很一致,,或者被它唤醒,
    % B6 D, N+ Y: \# I% Q$ _我推理机的核心算法应该是二叉树遍历的变种。
    # o# Z2 J0 R1 l4 S6 o0 H另外知识系统的推理机搜索深度(递归深度)并不长,没有超过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:
    7 o5 x) f) {% D( v0 i; \Key Idea of Recursion1 o8 a0 u. w4 `
    3 v: |0 X% s. ]' T: s0 D
    A recursive function solves a problem by:' X% o( s: L8 M% t; \! n* q+ P
    # ]/ }8 F4 \" J! c0 B5 ]1 E/ v
        Breaking the problem into smaller instances of the same problem.
    7 [" q! Q" D3 [0 ]2 N4 C8 B6 G9 {8 K
        Solving the smallest instance directly (base case).
    . y6 B* O; L+ H/ M4 [4 t
    $ x" y+ |7 o* u+ ?1 N/ e) V) U    Combining the results of smaller instances to solve the larger problem.
    4 n- t3 r0 g# F7 E
    0 _% ^$ g7 P/ J7 z( K: H+ b! ^( vComponents of a Recursive Function
    0 B4 X$ E& a: A/ h- m4 B# P5 J9 |* T) [; u# M/ v$ m5 h' P
        Base Case:: z3 _7 M0 _% C1 @: {4 S# I

    2 N' e/ N, K8 j  g$ ~. ]" z        This is the simplest, smallest instance of the problem that can be solved directly without further recursion.
    ( b8 b+ |( A- M6 w. c: r
    2 e3 F! H9 z! B" J! J9 C+ w1 m        It acts as the stopping condition to prevent infinite recursion.
    6 A# F+ \- G$ O* N- H, [. p+ ^4 ~1 Q+ I  Y- x$ T% o
            Example: In calculating the factorial of a number, the base case is factorial(0) = 1.
    ' S3 n6 C% b4 Y# F/ s
    ) {8 r' s" |! g, e' n3 |7 V    Recursive Case:  v" ~( G; h; b' g
    $ x% E3 x0 u* Q1 |3 q0 ~9 H
            This is where the function calls itself with a smaller or simpler version of the problem.
    - f& @6 b# ]" t& d0 j( m1 ]3 B: t2 u
    ) B1 e0 i6 \# [4 S        Example: For factorial, the recursive case is factorial(n) = n * factorial(n-1).# j  l% H! M8 a1 J# X5 M% y! x1 r0 h
    6 `) j6 ~4 s! l6 S7 z3 V3 L
    Example: Factorial Calculation/ X9 ?2 {- ?- e

    6 X) X/ Q" ]7 T% T2 oThe 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:6 W( y% G' d; C, |& Q4 y7 A6 c5 v
    ; l9 o( L& V3 p. c+ `
        Base case: 0! = 10 o2 ?; ~: o1 z# J8 k. ^2 }
    - ]; d4 s0 O, R' `
        Recursive case: n! = n * (n-1)!/ s5 T5 N* K: }: @& ^2 Y, h3 n4 U

      Q4 L1 I& q- v6 M* }Here’s how it looks in code (Python):
    7 C1 P' Q/ [- h/ |python
    % X% W. N. h$ z  p( w1 v6 \5 u; A. c9 d  H8 R- ]) W4 L3 r
    - N+ e8 B$ Y# }- U
    def factorial(n):" K9 \# x1 U- V7 w
        # Base case
    ) |* Z! S' q: E  N    if n == 0:% M5 i0 W! N! w9 S0 B# `7 O
            return 1. x5 l8 B9 g  ~
        # Recursive case
    0 o/ k: s2 E) n$ p8 u3 O    else:- A: A6 Z: J0 H  C+ D5 u
            return n * factorial(n - 1)
    + j3 e3 S! X1 Z4 J% J. p( s( ?
    % G8 U$ b% a; Y+ m# ?7 ^, X# Example usage
    8 P$ I7 \: `& ]print(factorial(5))  # Output: 1205 j, c! z1 ^2 e
    - [* r/ H2 b2 Q8 S% B& D
    How Recursion Works
    % X6 j5 J3 N. u2 o! d; }3 A1 D" _% {* G1 M1 P
        The function keeps calling itself with smaller inputs until it reaches the base case.
    * l. ~! ~# K9 m8 W) m
    . G! I8 ]  }- L4 b' }    Once the base case is reached, the function starts returning values back up the call stack.
      ]3 d. L  A8 K, q: r$ `- N+ ~  J, o
        These returned values are combined to produce the final result.# q! [" I  X2 G: k6 m
    ' P3 ~9 U& s  R$ x* L2 V
    For factorial(5):
    ( _0 G$ a& ]! b0 ^' Y1 I: r* }  p" ~- ~; d

    ( q  M1 x, b7 r- @8 }9 G0 G5 Y8 z7 wfactorial(5) = 5 * factorial(4)/ O& p3 U# @& l3 u; w4 x% y5 u+ v
    factorial(4) = 4 * factorial(3). Z) @* t: e4 g' u' H  K* k! D$ \+ _, n
    factorial(3) = 3 * factorial(2)
    , d% k! K0 z* a' Ifactorial(2) = 2 * factorial(1)$ H% A/ K) J; W- f" x: G, y
    factorial(1) = 1 * factorial(0)
      n! P" g1 e& [" `" T4 @factorial(0) = 1  # Base case
    ' H3 ~: R" M  Z4 u& u0 r3 j8 `% z+ A0 ^7 \0 [; ?7 [+ i
    Then, the results are combined:
    3 _( u$ l6 N6 ~. E- f! Q' _" W" j. O3 R, w" k( g( K5 f) }
    - M8 j/ X3 ^7 |
    factorial(1) = 1 * 1 = 1
    * x+ ]( \' a; G, Y$ [5 ?' f% pfactorial(2) = 2 * 1 = 2
    " v8 h0 V" I8 [, r8 N; a4 t/ y: Wfactorial(3) = 3 * 2 = 65 \* T5 T0 F( T6 }) _, i; `
    factorial(4) = 4 * 6 = 24+ k  i; i2 q3 B* R* ]
    factorial(5) = 5 * 24 = 120; A2 s, b4 s; n" u
    : p/ ^1 E0 u$ m0 _# H% K0 r
    Advantages of Recursion
    : c: t3 h" T$ {$ z/ m5 t+ b; f' X0 _. [0 E/ ]. H$ p5 I7 u
        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).9 \- n! I; ?+ a

    & J' n+ i( i. y7 n$ o2 W% }    Readability: Recursive code can be more readable and concise compared to iterative solutions.1 _/ v# F" ~  n3 g/ `( ?$ y
    ( E- l1 u8 @0 M- k( y5 H
    Disadvantages of Recursion
    * ]6 l+ u6 o  p% U. B9 v0 X1 u, i' `& h, |# G" E9 r
        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.
    . z2 h- B' t8 f4 N) d" z; S1 X- b6 n( b# @. W
        Inefficiency: Some problems can be solved more efficiently using iteration (e.g., Fibonacci sequence without memoization).$ S+ ~: k1 \8 w1 @
    * A( ~4 Q8 z8 D& b. n1 x. N
    When to Use Recursion# @7 M3 O1 Y7 J
    / A3 R# S* Q+ L- s( o
        Problems that can be broken down into smaller, similar subproblems (e.g., tree traversals, sorting algorithms like quicksort and mergesort).! ~; i3 {5 H" p0 Y( D
    ! q8 D& G% e# Q' Z
        Problems with a clear base case and recursive case.9 |$ r; R7 d5 |: V9 u% z1 \
    ( S, V1 D$ T/ \' u! ^* F. A4 T: b1 Q
    Example: Fibonacci Sequence6 |* g9 x1 A! t
    9 x6 ]" w: X- v7 V  z. B2 Z
    The Fibonacci sequence is another classic example of recursion. Each number is the sum of the two preceding ones:9 W! z7 U5 m. y
    ' @4 t) z4 D; J: B3 [  ^
        Base case: fib(0) = 0, fib(1) = 1
    - X* n3 k. q# m8 [" Q
    , b( ?6 x: E' x2 U+ P1 Y+ O    Recursive case: fib(n) = fib(n-1) + fib(n-2)2 N! M2 |8 b2 `( h$ j6 M

    * e6 H, _6 H5 V; Apython5 x. C3 s" Y( X- ?& h" r6 A; O
    5 w2 W1 W. E  I+ P5 X* L7 E: E4 I" B
    + A2 b# ~' }5 Q1 g9 q2 V
    def fibonacci(n):
    % R1 [3 e" c* d5 X& B    # Base cases3 Z$ ?; f4 K6 a. Y7 b; R! ~* t
        if n == 0:1 Y" r* A" A2 v7 [; b! j+ i" A
            return 0
    , J% y1 J" X7 h: v$ D    elif n == 1:0 K% T- \# \8 n; @2 I8 }
            return 10 a' B4 v5 J; @4 R; i7 G' n
        # Recursive case
    / r& ?9 {5 V% H2 Q6 ^7 q. ]' r9 W    else:
    & L. f8 J3 M4 F. d        return fibonacci(n - 1) + fibonacci(n - 2)
    " x7 C! x6 E" O1 O6 Y8 b9 d
    2 n: t' q2 \/ U: l# Example usage
    5 I/ ]) p& s. l& r+ j- o  k2 Q; Tprint(fibonacci(6))  # Output: 8
    * t  c8 |5 p( a( ?' [% Z9 J/ X- o% `4 d! l
    Tail Recursion
    , v1 T7 L: v, A8 |: w9 z: Q; j  ]$ e; B4 E7 Z& A
    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).5 T7 Q: R6 l- R9 S0 s1 v
    + H: y  {  T+ M* D$ W+ u8 c) {
    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-14 21:21 , Processed in 0.032675 second(s), 19 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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