Skip to content

Instantly share code, notes, and snippets.

@yllan
Last active December 24, 2019 02:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yllan/0280c3f6b4f8a92016b70b02cb17acbf to your computer and use it in GitHub Desktop.
Save yllan/0280c3f6b4f8a92016b70b02cb17acbf to your computer and use it in GitHub Desktop.
遞迴思考 Thinking in recursion
  1. 用遞迴寫 n!。 factorial(5) = 5 * 4 * 3 * 2 * 1。 Q: 終止條件是什麼?
  2. 用遞迴寫費氏數列,F(0) = 1, F(1) = 1, F(2) = F(1) + F(0), F(3) = F(2) + F(1)
  3. 輸入 n,產生 n 個數的所有排列: P(3) = [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
  4. 輸入 n, k,產生所有 n 取 k 個的組合: C(5, 3) = [[1, 2, 3], [1, 2, 4], [1, 2, 5], [2, 3, 4,], [2, 3, 5], [3, 4, 5]]
  5. 輸入 n, 輸出所有 n 的分割法。 S(5) = [[5], [4, 1], [3, 2], [3, 1, 1], [2, 2, 1], [2, 1, 1, 1], [1, 1, 1, 1, 1]]
  6. 圓周上有 n 個點,有幾種方法可以三角化? C(7) = 42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment