question:
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.
answer:(which don‘t think by myself)
思路:
举例:
38,11,2 38-2=36(38-11=27,11-2=9,都是9的倍数)
57,12,3 57-3=54(57-12=45,12-3=9,都是9的倍数)
所以直接用57/9 得余数为3 ??
其实不对.......忘记了特殊情况(就是整除的时候)
18,9
余数为0,不符合上述方法
所以需要先减去一个数,最后余数再加上一个数,可以避免这种情况,但是可以随意先加后减一个数吗??
不可以,举例10-2=8 8对9取余数为8,8+2=10,不符合条件
所以只能是1
时间: 2024-10-12 16:59:57