Python 基礎知識簡單

阿姆斯壯數

“阿姆斯特朗數”問題的詳細指南和 Python 實作。

問題陳述

簡單

寫一個函數 is_armstrong(n) ,它接受正整數 n 並傳回 True (如果它是阿姆斯壯數),否則傳回 False 。阿姆斯壯數(自戀數)是一個等於其自身數字總和的數字的位數次方的數字。例如,153 有 3 位數字,1^3 + 5^3 + 3^3 = 153。

約束條件
  • 1 <= n <= 10^6

範例

Example 1
Input
n = 153
Output
True
Explanation

153 has 3 digits. 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153. So it is an Armstrong number.

Example 2
Input
n = 370
Output
True
Explanation

370 has 3 digits. 3^3 + 7^3 + 0^3 = 27 + 343 + 0 = 370.

Example 3
Input
n = 123
Output
False
Explanation

123 has 3 digits. 1^3 + 2^3 + 3^3 = 1 + 8 + 27 = 36, which is not 123.

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 資源

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