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

Neon Number

Check whether the digit sum of a number's square equals the number.

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

  • Square a number without overflow
  • Sum the digits of the square
  • Compare the result with the input

โจทย์

ดูฉบับแปล

A neon number is a number whose square has a digit sum equal to the number itself. For example, 9² = 81 and 8 + 1 = 9, so 9 is a neon number.

Implement neon_number(n).

n² can reach 10^18, which fits in a signed 64-bit integer.

Input

n: The number to test.

Output

Return true if the sum of the decimal digits of n² equals n, otherwise false.

เงื่อนไข

0 ≤ n ≤ 10^9

ตัวอย่าง

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

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

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

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

คำอธิบาย

9² = 81 and 8 + 1 = 9, so the answer is true.

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

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

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

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

คำอธิบาย

12² = 144 and 1 + 4 + 4 = 9, which is not 12, so the answer is false.

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

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

คำใบ้ 1

Compute n × n in a 64-bit integer.

คำใบ้ 2

Add up the digits of the square with % 10 and / 10.

คำใบ้ 3

The digit sum of a square below 10^18 is at most 162, so large n can never be neon.

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

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

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

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

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

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

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

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

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