原题地址: leetcode-cn.com/problems/co…
class Solution {
public int countEven(int num) {
int count = 0, index = 2;
while (index <= num) {
int temp = index;
int i = 0;
while (temp != 0) {
i += temp % 10;
temp = temp / 10;
}
if (i % 2 == 0) {
count ++;
}
index ++;
}
return count;
}
}
思路: 遍历直接从2开始, 对每一位数相加判断即可