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

Automorphic Number

Check whether the square of a number ends with the number itself.

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

  • Count the digits of a number
  • Take the last d digits with a modulo
  • Handle 0 as a one-digit number

Đề bài

Xem bản dịch

A number is automorphic if its square ends in the same digits as the number. For example, 76² = 5776, which ends in 76, so 76 is automorphic.

Implement automorphic_number(n).

n² can be as large as 10^18, which still fits in a signed 64-bit integer. You can compare digits from the right, or compare n² modulo 10^d with n, where d is the number of digits of n.

Input

n: The number to test.

Output

Return true if the last digits of n² are exactly the digits of n, otherwise false. 0 and 1 are automorphic (0² = 0, 1² = 1).

Ràng buộc

0 ≤ n ≤ 10^9

Ví dụ

Ví dụ 1
Đầu vào:
n = 25

Kết quả mong đợi:
expected = True
Đầu vào:
long long n = 25;

Kết quả mong đợi:
bool expected = true;
Đầu vào:
long n = 25;

Kết quả mong đợi:
bool expected = true;

Giải thích

25² = 625, which ends in 25, so the answer is true.

Ví dụ 2
Đầu vào:
n = 7

Kết quả mong đợi:
expected = False
Đầu vào:
long long n = 7;

Kết quả mong đợi:
bool expected = false;
Đầu vào:
long n = 7;

Kết quả mong đợi:
bool expected = false;

Giải thích

7² = 49, which ends in 9, not 7, so the answer is false.

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

Find how many digits n has.

Gợi ý 2

The last d digits of a number are its remainder modulo 10^d.

Gợi ý 3

Make sure n = 0 counts as one digit, so 10^d is 10, not 1.

Độ phức tạp mong đợi

O(d) time for d digits, 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.

Lời giải đạt tốt nhất của mỗi người. Lời giải nhanh hơn, tốn ít bộ nhớ hơn và được đánh giá cao hơn sẽ đứng trên.

Chưa có lời giải nào đạt. Giải bài này để giành hạng nhất.

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.