2180. 统计各位数字之和为偶数的整数个数
为保证权益,题目请参考 2180. 统计各位数字之和为偶数的整数个数(From LeetCode).
解决方案1
CPP
C++
class Solution {
public:
int countEven(int num) {
int count = 0;
for (int n = 1; n <= num; n++) {
if (this->valid(n)) {
count += 1;
}
}
return count;
}
bool valid(int n) {
int s = 0;
while (n != 0) {
s += n % 10;
n = n / 10;
}
return s % 2 == 0;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21