#207. 递归计算数字各位之和

    ID: 207 Type: Default 1000ms 256MiB Tried: 0 Accepted: 0 Difficulty: (None) Uploaded By: Tags>codingbatWarmup-2gesp5递归

递归计算数字各位之和

Sum of Digits (Recursive)

Background

Congcong is currently learning about recursion. He encountered an interesting problem that requires calculating the sum of digits of a non-negative integer, but it must be done recursively, without using loops.

Problem Description

Given a non-negative integer nn, return the sum of its digits recursively (no loops). Note that modulo (% 10) by 1010 yields the rightmost digit (e.g., 126%10126 \% 10 is 66), while integer division (/ 10) by 1010 removes the rightmost digit (e.g., 126/10126 / 10 is 1212).

Input Format

Input is given from Standard Input in the following format.

A non-negative integer nn.

Output Format

Output is printed to Standard Output in the following format.

The sum of the digits of nn.

Sample

126
9
49
13
12
3

Sample Explanation

For input 126126: The last digit of 126126 is 66 (126%10=6126 \% 10 = 6). The remaining part is 1212 (126/10=12126 / 10 = 12). Recursively calculate the sum of digits for 1212, which is 33. The final result is 6+3=96 + 3 = 9.

Constraints

0n<2310 \le n < 2^{31}. Time limit: 11 second, Memory limit: 10241024 MiB.