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
新闻热点
疑难解答
图片精选