Đề bài
Xem bản dịchAdd 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.
Ràng buộc
1 ≤ n ≤ 10¹⁸
Ví dụ
Ví dụ 1
Đầu vào:
n = 999
Kết quả mong đợi:
expected = 9
Đầu vào:
long long n = 999;
Kết quả mong đợi:
long long expected = 9;
Đầu vào:
long n = 999;
Kết quả mong đợi:
long expected = 9;
Giải thích
9 + 9 + 9 = 27, then 2 + 7 = 9.
Ví dụ 2
Đầu vào:
n = 38
Kết quả mong đợi:
expected = 2
Đầu vào:
long long n = 38;
Kết quả mong đợi:
long long expected = 2;
Đầu vào:
long n = 38;
Kết quả mong đợi:
long expected = 2;
Giải thích
3 + 8 = 11, then 1 + 1 = 2.