class Solution { public: int DigitSum(int num){ int sum=0; do{ sum=sum+num%10; num=num/10; }while(num!=0) return sum; } int addDigits(int num) { int n=num; while(n>=10){ num=DigitSum(n); n=num; } return num; } };
Given a non-negative integer num
, repeatedly add all its digits until the result has only one digit.
哪里不对?