ง่าย 0 ข้อที่แก้แล้ว อัตราผ่าน 0%

Automorphic Number

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

สิ่งที่คุณจะได้เรียนรู้

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

โจทย์

ดูฉบับแปล

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).

เงื่อนไข

0 ≤ n ≤ 10^9

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
n = 25

ผลลัพธ์ที่คาดหวัง:
expected = True
อินพุต:
long long n = 25;

ผลลัพธ์ที่คาดหวัง:
bool expected = true;
อินพุต:
long n = 25;

ผลลัพธ์ที่คาดหวัง:
bool expected = true;

คำอธิบาย

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

ตัวอย่างที่ 2
อินพุต:
n = 7

ผลลัพธ์ที่คาดหวัง:
expected = False
อินพุต:
long long n = 7;

ผลลัพธ์ที่คาดหวัง:
bool expected = false;
อินพุต:
long n = 7;

ผลลัพธ์ที่คาดหวัง:
bool expected = false;

คำอธิบาย

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

แนวทางการแก้

ลองทำเองก่อน แล้วค่อยเปิดคำใบ้ทีละข้อ

คำใบ้ 1

Find how many digits n has.

คำใบ้ 2

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

คำใบ้ 3

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

ความซับซ้อนที่คาดหวัง

O(d) time for d digits, O(1) extra space

แหล่งอ้างอิง

โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

คำตอบที่ผ่านดีที่สุดของแต่ละคน คำตอบที่เร็วกว่า ใช้หน่วยความจำน้อยกว่า และได้คะแนนดีกว่าจะอยู่ด้านบน

ยังไม่มีคำตอบที่ผ่าน แก้โจทย์นี้แล้วคว้าอันดับหนึ่งไปเลย

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก