โจทย์
ดูฉบับแปลAdd up the digits of n. If the result has more than one digit, add up its digits again. Repeat until a single digit remains.
Implement digital_root(n).
Input
n: A positive integer.
Output
Return the final single digit.
เงื่อนไข
1 ≤ n ≤ 10¹⁸
ตัวอย่าง
ตัวอย่างที่ 1
อินพุต:
n = 999
ผลลัพธ์ที่คาดหวัง:
expected = 9
อินพุต:
long long n = 999;
ผลลัพธ์ที่คาดหวัง:
long long expected = 9;
อินพุต:
long n = 999;
ผลลัพธ์ที่คาดหวัง:
long expected = 9;
คำอธิบาย
9 + 9 + 9 = 27, then 2 + 7 = 9.
ตัวอย่างที่ 2
อินพุต:
n = 38
ผลลัพธ์ที่คาดหวัง:
expected = 2
อินพุต:
long long n = 38;
ผลลัพธ์ที่คาดหวัง:
long long expected = 2;
อินพุต:
long n = 38;
ผลลัพธ์ที่คาดหวัง:
long expected = 2;
คำอธิบาย
3 + 8 = 11, then 1 + 1 = 2.