Dễ 0 đã giải Tỷ lệ chấp nhận 0%

Armstrong Number

Check whether a number equals the sum of its digits raised to the digit count.

Bạn sẽ học được gì

  • Count the digits of a number
  • Raise each digit to the digit count and add them up
  • Compare the result with the original number

Đề bài

Xem bản dịch

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ụ

Ví dụ 1
Đầ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.

Ví dụ 2
Đầ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.

Học hỏi từ người khác

Giải pháp cộng đồng

Chưa có giải pháp nào được chia sẻ. Hãy giải thử thách này để đăng giải pháp đầu tiên.