258: Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
For example:
Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.
【题目大意】给定一个数,不断地把这个数的每个数位相加,直到这个数变成一位数
【解法】数位分离,每次分离出个位数加到原数字上,直到只剩一个数位
【AC代码】
int addDigits(int num) {
while(num >= 10){
num = num / 10 + num % 10;
}
return num;
}