add: 220208

This commit is contained in:
Lam Haoyin 2022-02-08 11:03:21 +08:00
parent ae07a21c67
commit d21f130996
No known key found for this signature in database
GPG Key ID: 8C089CB1A2B7544F
2 changed files with 22 additions and 1 deletions

21
2202/220208.cpp Normal file
View File

@ -0,0 +1,21 @@
#include <iostream>
/**
* 258. Add Digits
* Given an integer `num`, repeatedly add all its digits until the result has only one digit, and return it.
*/
class Solution {
public:
static constexpr int addDigits(int num) {
int ret = 0;
for (; num; num /= 10)
ret += num % 10;
return ret < 10 ? ret : addDigits(ret);
}
};
int main() {
std::cout << Solution::addDigits(38);
return 0;
}

View File

@ -3,4 +3,4 @@ PROJECT(2202)
SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2202 220207.cpp) ADD_EXECUTABLE(2202 220208.cpp)