Skip to content

Latest commit

 

History

History
42 lines (36 loc) · 746 Bytes

2180.统计各位数字之和为偶数的整数个数.md

File metadata and controls

42 lines (36 loc) · 746 Bytes

2180.统计各位数字之和为偶数的整数个数

/*
 * @lc app=leetcode.cn id=2180 lang=typescript
 *
 * [2180] 统计各位数字之和为偶数的整数个数
 */

// @lc code=start
function countEven(num: number): number {}
// @lc code=end

解法 1: 枚举

function countEven(num: number): number {
  let res = 0
  for (let i = 1; i <= num; i++) {
    let x = i,
      ans = 0
    while (x) {
      ans += x % 10
      x = Math.floor(x / 10)
    }
    if (ans % 2 === 0) res++
  }
  return res
}

Case

test.each([
  { input: { num: 4 }, output: 2 },
  { input: { num: 30 }, output: 14 },
])('input: num = $input.num', ({ input: { num }, output }) => {
  expect(countEven(num)).toEqual(output)
})