diff --git a/2202/220208.cpp b/2202/220208.cpp new file mode 100644 index 0000000..885f1d1 --- /dev/null +++ b/2202/220208.cpp @@ -0,0 +1,21 @@ +#include + +/** + * 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; +} diff --git a/2202/CMakeLists.txt b/2202/CMakeLists.txt index 717308c..7bae653 100644 --- a/2202/CMakeLists.txt +++ b/2202/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2202) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2202 220207.cpp) +ADD_EXECUTABLE(2202 220208.cpp)