
这道题涉及到数学领域的一个定理,即“数字根”。

解法一:笨方法(循环)
public int addDigits(int num) {
Integer res = new Integer(num);
while(res>9){
String str = res.toString();
int temp = 0;
for(int i=0;i<str.length();i++){
temp+=Integer.parseInt(str.substring(i,i+1));
}
res = new Integer(temp);
}
return res;
}
解法二:公式法
public int addDigits(int num) {
return (num-1)%9+1;
}

本文深入探讨了数学领域中数字根的概念,提供了两种计算数字根的有效方法:循环法和公式法。循环法通过不断累加数字直至得到一位数来求解,而公式法则直接应用数学公式快速得出结果。
800

被折叠的 条评论
为什么被折叠?



