Đề bài
A number with k digits is an Armstrong number if the sum of each digit raised to the power k equals the number. For example 153 = 1³ + 5³ + 3³.
Implement is_armstrong(n).
Input
n: A non-negative integer.
Output
Return true if n is an Armstrong number.
Ràng buộc
0 ≤ n ≤ 10¹⁸
Ví dụ
Đầu vào:
n = 153
Kết quả mong đợi:
expected = True
Đầu vào:
long long n = 153;
Kết quả mong đợi:
bool expected = true;
Đầu vào:
long n = 153;
Kết quả mong đợi:
bool expected = true;
Giải thích
1³ + 5³ + 3³ = 1 + 125 + 27 = 153.
Đầu vào:
n = 10
Kết quả mong đợi:
expected = False
Đầu vào:
long long n = 10;
Kết quả mong đợi:
bool expected = false;
Đầu vào:
long n = 10;
Kết quả mong đợi:
bool expected = false;
Giải thích
1² + 0² = 1, which is not 10.
Hướng tiếp cận
Hãy tự thử trước, sau đó mở từng gợi ý một.
Gợi ý 1
Count how many digits the number has before summing powers.
Gợi ý 2
Compare the original number with the sum of each digit raised to that digit count.
Gợi ý 3
Remember that 0 is a special one-digit case.
Độ phức tạp mong đợi
O(k) time, O(1) extra space
Tham khảo
Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.