返回介绍

solution / 2100-2199 / 2180.Count Integers With Even Digit Sum / README_EN

发布于 2024-06-17 01:03:09 字数 4295 浏览 0 评论 0 收藏 0

2180. Count Integers With Even Digit Sum

中文文档

Description

Given a positive integer num, return _the number of positive integers less than or equal to_ num _whose digit sums are even_.

The digit sum of a positive integer is the sum of all its digits.

 

Example 1:

Input: num = 4
Output: 2
Explanation:
The only integers less than or equal to 4 whose digit sums are even are 2 and 4.  

Example 2:

Input: num = 30
Output: 14
Explanation:
The 14 integers less than or equal to 30 whose digit sums are even are
2, 4, 6, 8, 11, 13, 15, 17, 19, 20, 22, 24, 26, and 28.

 

Constraints:

  • 1 <= num <= 1000

Solutions

Solution 1

class Solution:
  def countEven(self, num: int) -> int:
    ans = 0
    for x in range(1, num + 1):
      s = 0
      while x:
        s += x % 10
        x //= 10
      ans += s % 2 == 0
    return ans
class Solution {
  public int countEven(int num) {
    int ans = 0;
    for (int i = 1; i <= num; ++i) {
      int s = 0;
      for (int x = i; x > 0; x /= 10) {
        s += x % 10;
      }
      if (s % 2 == 0) {
        ++ans;
      }
    }
    return ans;
  }
}
class Solution {
public:
  int countEven(int num) {
    int ans = 0;
    for (int i = 1; i <= num; ++i) {
      int s = 0;
      for (int x = i; x; x /= 10) {
        s += x % 10;
      }
      ans += s % 2 == 0;
    }
    return ans;
  }
};
func countEven(num int) (ans int) {
  for i := 1; i <= num; i++ {
    s := 0
    for x := i; x > 0; x /= 10 {
      s += x % 10
    }
    if s%2 == 0 {
      ans++
    }
  }
  return
}
function countEven(num: number): number {
  let ans = 0;
  for (let i = 1; i <= num; ++i) {
    let s = 0;
    for (let x = i; x; x = Math.floor(x / 10)) {
      s += x % 10;
    }
    if (s % 2 == 0) {
      ++ans;
    }
  }
  return ans;
}

Solution 2

class Solution:
  def countEven(self, num: int) -> int:
    ans = num // 10 * 5 - 1
    x, s = num // 10, 0
    while x:
      s += x % 10
      x //= 10
    ans += (num % 10 + 2 - (s & 1)) >> 1
    return ans
class Solution {
  public int countEven(int num) {
    int ans = num / 10 * 5 - 1;
    int s = 0;
    for (int x = num / 10; x > 0; x /= 10) {
      s += x % 10;
    }
    ans += (num % 10 + 2 - (s & 1)) >> 1;
    return ans;
  }
}
class Solution {
public:
  int countEven(int num) {
    int ans = num / 10 * 5 - 1;
    int s = 0;
    for (int x = num / 10; x > 0; x /= 10) {
      s += x % 10;
    }
    ans += (num % 10 + 2 - (s & 1)) >> 1;
    return ans;
  }
};
func countEven(num int) (ans int) {
  ans = num/10*5 - 1
  s := 0
  for x := num / 10; x > 0; x /= 10 {
    s += x % 10
  }
  ans += (num%10 + 2 - (s & 1)) >> 1
  return
}
function countEven(num: number): number {
  let ans = Math.floor(num / 10) * 5 - 1;
  let s = 0;
  for (let x = Math.floor(num / 10); x; x = Math.floor(x / 10)) {
    s += x % 10;
  }
  ans += ((num % 10) + 2 - (s & 1)) >> 1;
  return ans;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文