作者:小迅
链接:https://leetcode.cn/problems/count-integers-with-even-digit-sum/solutions/2047389/mo-ni-zhu-shi-chao-ji-xiang-xi-by-xun-ge-tk8n/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
题目
思路
题意 -> 求 1 - num 的集合中,各位数之和为 偶数 的个数
直接按照题目要求枚举 1 - num 中的每一个元素,将每一个元素的各个位置的数进行累和,最后判断是否为偶数即可
代码注释超级详细
代码
volatile static int sum(int n)//各个数累和
{
int count = 0;
while (n) {
count += n % 10;
n /= 10;
}
return (count % 2 == 0 ? 1 : 0);//判断是否为偶数
}
int countEven(int num){
int count = 0;
for (int i = 1; i <= num; ++i) {//枚举每一个元素
count += sum(i);//累和
}
return count;
}
作者:小迅
链接:https://leetcode.cn/problems/count-integers-with-even-digit-sum/solutions/2047389/mo-ni-zhu-shi-chao-ji-xiang-xi-by-xun-ge-tk8n/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。