内容中心

2026年口碑好的皮带机托辊支架多家厂家对比分析-沧州路博机械制造有限公司

To solve this problem, we need to count the number of times the digit 1 appears in all non-negative integers from 0 to N. The key insight is to calculate the number of 1s in each digit position (units, tens, hundreds, etc.) separately and sum these counts.

Approach

For each digit position (starting from the units place), we consider three components:

  1. Higher: The number formed by the digits to the left of the current position.
  2. Current: The digit at the current position.
  3. Lower: The number formed by the digits to the right of the current position.
  4. Power: 10 raised to the power of the current position (e.g., 1 for units, 10 for tens, etc.).

The count of 1s in the current position depends on the value of the current digit:

  • Current < 1: The count is higher * power (since the higher part can range from 0 to higher-1, and the lower part can be anything).
  • Current == 1: The count is higher * power + lower + 1 (includes all cases from the previous scenario plus the cases where the higher part is exactly the current higher value and the lower part ranges from 0 to lower).
  • Current > 1: The count is (higher + 1) * power (since the higher part can range from 0 to higher, and the lower part can be anything).

Solution Code

def count_digit_one(n):
    count = 0
    power = 1
    while power <= n:
        higher = n // (power * 10)
        current = (n // power) % 10
        lower = n % power
        if current < 1:
            count += higher * power
        elif current == 1:
            count += higher * power + lower + 1
        else:
            count += (higher + 1) * power
        power *= 10
    return count

Explanation

Let's take an example to see how this works:

  • N = 13:
    • Units place: Current digit is 3 (>1). Count = (1+1)*1 = 2 (1,11).
    • Tens place: Current digit is 1. Count = (0*10) + (3+1) =4 (10,11,12,13).
    • Total: 2+4=6 (correct).

Another example: N=21:

  • Units place: Current digit is1. Count = (2*1)+(0+1)=3 (1,11,21).
  • Tens place: Current digit is 2 (>1). Count = (0+1)*10=10 (10-19).
  • Total:3+10=13 (correct).

This approach efficiently computes the count in O(log N) time complexity (since we process each digit position once), making it suitable for large values of N.

Answer: The function count_digit_one(n) returns the number of times the digit 1 appears in all numbers from 0 to N. For example, count_digit_one(13) returns 6.

The final answer is the code provided above. However, if you need to input a specific N and get the result, you can call the function with that N. For instance, if N=13, the answer is 6.

But as per the problem's requirement (since the user might expect the code), the solution is the function itself. If the problem expects a numerical answer for a specific N, please provide N, but based on the common problem, the code is the solution.

Assuming the problem expects the code, the answer is the code given.

def count_digit_one(n):
    count = 0
    power = 1
    while power <= n:
        higher = n // (power * 10)
        current = (n // power) % 10
        lower = n % power
        if current < 1:
            count += higher * power
        elif current == 1:
            count += higher * power + lower + 1
        else:
            count += (higher + 1) * power
        power *= 10
    return count

沧州路博机械制造有限公司

沧州路博机械制造有限公司



作者声明:本文包含人工智能生成内容。

在线客服

在线留言
您好,很高兴为您服务,可以留下您的电话或微信吗?