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

Disarium Number

Check whether a number equals the sum of its digits raised to their positions.

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

  • Read digits from left to right
  • Raise a digit to its position with integer arithmetic
  • Add powers without overflow

โจทย์

ดูฉบับแปล

Number the digits of n from the left starting at 1, raise each digit to the power of its position, and add the results. If the total is n itself, the number is a disarium number. For example, 175 gives 1¹ + 7² + 5³ = 1 + 49 + 125 = 175.

Implement disarium_number(n).

Input

n: The number to test.

Output

Return true if n is a disarium number, otherwise false. Every single-digit number is a disarium number.

เงื่อนไข

1 ≤ n ≤ 10^9

ตัวอย่าง

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

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

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

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

คำอธิบาย

8¹ + 9² = 8 + 81 = 89, so the answer is true.

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

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

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

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

คำอธิบาย

9¹ + 8² = 9 + 64 = 73, not 98, so the answer is false.

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

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

คำใบ้ 1

Positions count from the left, so either turn n into a string or count the digits first.

คำใบ้ 2

Use integer multiplication for powers instead of a floating point function.

คำใบ้ 3

The largest term is 9^10, which fits easily in 64 bits.

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

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

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

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

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

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

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

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

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