Zheaoli / do-something-right

MIT License
37 stars 3 forks source link

2023-01-06 #474

Open Zheaoli opened 1 year ago

Zheaoli commented 1 year ago

2023-01-06

dreamhunter2333 commented 1 year ago
#include <iostream>
using namespace std;
/*
 * @lc app=leetcode.cn id=2180 lang=cpp
 *
 * [2180] 统计各位数字之和为偶数的整数个数
 */

// @lc code=start
class Solution
{
public:
    int countEven(int num)
    {
        int res = 0;
        for (size_t i = 1; i <= num; i++)
        {
            int sum_i = 0;
            int tmp = i;
            while (tmp > 0)
            {
                sum_i += tmp % 10;
                tmp = tmp / 10;
            }
            if (sum_i % 2 == 0)
                res += 1;
        }
        return res;
    }
};
// @lc code=end
int main(int argc, char const *argv[])
{
    Solution s;
    cout << s.countEven(4) << endl;
    cout << s.countEven(30) << endl;
    return 0;
}

微信id: 而我撑伞 来自 vscode 插件