题目连接
https://leetcode.com/problems/add-digits/
Add Digits
Description
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.
class Solution {
public:
int addDigits(int num) {
while (num >= 10) {
int ret = 0;
do ret += num % 10; while (num /= 10);
num = ret;
}
return num;
}
};
本文解析了LeetCode上的一道经典题目:Add Digits。该题要求对一个非负整数的所有位进行反复求和,直到结果为一位数为止。文章提供了C++实现代码,展示了如何通过循环迭代的方法解决这一问题。
1316

被折叠的 条评论
为什么被折叠?



