diff options
Diffstat (limited to 'other/count_ints_even_sum.cpp')
-rw-r--r-- | other/count_ints_even_sum.cpp | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/other/count_ints_even_sum.cpp b/other/count_ints_even_sum.cpp new file mode 100644 index 0000000..83070c6 --- /dev/null +++ b/other/count_ints_even_sum.cpp @@ -0,0 +1,26 @@ +// 2180. Count Integers With Even Digit Sum +#include "leetcode.h" + +/* + * given a positive integer 'num', return the number of positive integersless + * than or equal to 'num' whose digit sums are event. the digitsum of a positive + * integer is the sum of all of its digits. + */ + +class Solution { +public: + int countEven(int num) { + int tmp = num, sum = 0; + while (num > 0) { + sum += num % 10; + num /= 10; + } + return sum % 2 == 0 ? tmp / 2 : (tmp - 1) / 2; + } +}; + +int main() { + Solution obj; + printf("%d\n", obj.countEven(4)); // expect: 2 + printf("%d\n", obj.countEven(30)); // expect: 14 +} |