Python 基礎知識簡單

階乘中最後一個非零數字

「階乘中最後一個非零數字」問題的詳細指南和 Python 實作。

問題陳述

簡單

寫一個函數 last_non_zero_digit(n) 傳回 n 的最後一個非零數字! (n 階乘)。例如,5! = 120,最後一個非零數字是 2。此函數採用非負整數 n 並傳回一個數字 (1-9)。

約束條件
  • 0 <= n <= 1000

範例

Example 1
Input
n = 5
Output
2
Explanation

5! = 120. Ignoring trailing zeros, the last non-zero digit is 2.

Example 2
Input
n = 10
Output
8
Explanation

10! = 3628800. The last non-zero digit is 8.

Example 3
Input
n = 1
Output
1
Explanation

1! = 1. The last non-zero digit is 1.

Need a Hint?
考慮使用特定於遞歸的資料結構,例如集合或堆。
Edge Cases to Watch
  • 空輸入結構
  • 單元素輸入
  • 大數值範圍

準備好解決了嗎?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

在編輯器中開啟
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推薦的 Python 資源

透過相關的互動式教學、備忘單和程式碼比較來擴展您的知識。