#各位相加问题
-方法1:公式法,把1-20数字列出来即可以找到规律
int addDigits(int num) {
return (num-1)%9+1;
}
-方法2:递归法,/10能降低数字的阶数
int addDigits(int num){
if (num >=0 && num < 10)
return num;
int next = 0;
//降低阶数
while (num != 0){
next = next + num % 10;
num = num / 10;
}
return addDigits(next);
}
-方法3:迭代法
int addDigits(int num){
int next = 0;
while (num >= 10)
{
while (num != 0)
{
next = next + num % 10;
num = num / 10;
}
num = next;
next = 0;
}
return num;
}