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.
給定一個非負整數num,重復地添加所有其數字,直到結果只有一個數字。
給定num = 38,過程如下:3 + 8 = 11,1 + 1 = 2。由于2只有一個數字,返回它。
http://blog.csdn.net/sbitswc/article/details/47975581 轉載自這位大神 不得不服,不用公式都代碼用的那么熟練
public int addDigits(int num) { while(num>=10){ num = (num/10)+num%10; } return num; }公式:
public int addDigits(int num) { return (num-1)%9 + 1 ; }官方給出的提示: https://en.wikipedia.org/wiki/Digital_root
新聞熱點
疑難解答
圖片精選