add: 220208
This commit is contained in:
parent
ae07a21c67
commit
d21f130996
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
@ -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)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue