Add one's place (thinking)

Title
Given a non-negative integer num, repeatedly add the digits in each digit until the result is a single digit.
For a non-negative number, its composition is 1 0 i a i = ( 1 0 i 1 ) a i + a i \sum{10^i*a_i} = \sum{(10^i-1)*a_i}+\sum{a_i} , The previous part is a multiple of 9, we can remove it by modulo 9, in order to prevent the answer itself from being 9, we need to -1 first, then modulo.

class Solution {
public:
    int addDigits(int num) {
        return (num-1)%9+1;
    }
};
Published 152 original articles · won praise 2 · Views 6436

Guess you like

Origin blog.csdn.net/weixin_43918473/article/details/105468174