258. Add Digits

来源:互联网 发布:sql注入绕过安全狗 编辑:程序博客网 时间:2024/05/01 18:15

本来想看看这样行不行,结果一下就AC了,超开心

可能这道题就是十位和个位吧

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 = 111 + 1 = 2. Since 2 has only one digit, return it.

int addDigits(int num) {    while(num/10!=0){        int a=0,b=0;        a=num%10;        b=(num-num%10)/10;        num=a+b;    }    return num;}


0 0
原创粉丝点击