【问题标题】:Number of occurrences of 2 as a digit in numbers from 0 to n , Not getting the O(n) solution?2 作为从 0 到 n 的数字中的数字出现的次数,没有得到 O(n) 解决方案?
【发布时间】:2020-11-24 06:29:07
【问题描述】:

This the GFG Link 在此链接中,我无法直观地了解我们如何将 2 的数量计算为数字, 我的疑问是,如果我们按照下面的描述计算范围内的 6000 位数字,那么为什么我们只是将数字除以 10 并返回它,如果有人可以帮助我,请用示例发布您的答案

Case digits < 2
Consider the value x = 61523 and digit at index d = 3 (here indexes are considered from right and rightmost index is 0). We observe that x[d] = 1. There are 2s at the 3rd digit in the ranges 2000 – 2999, 12000 – 12999, 22000 – 22999, 32000 32999, 42000 – 42999, and 52000 – 52999. So there are 6000 2’s total in the 3rd digit. This is the same amount as if we were just counting all the 2s in the 3rd digit between 1 and 60000.
In other words, we can round down to the nearest 10d+1, and then divide by 10, to compute the number of 2s in the d-th digit.
if x[d) < 2: count2sinRangeAtDigit(x, d) =
  Compute y = round down to nearest 10d+1 
  return y/10 
Case digit > 2
Now, let’s look at the case where d-th digit (from right) of x is greater than 2 (x[d] > 2). We can apply almost the exact same logic to see that there are the same number of 2s in the 3rd digit in the range 0 – 63525 as there as in the range 0 – 70000. So, rather than rounding down, we round up.
if x[d) > 2: count2sinRangeAtDigit(x, d) =
  Compute y = round down to nearest 10d+1 
  return y / 10 
Case digit = 2
The final case may be the trickiest, but it follows from the earlier logic. Consider x = 62523 and d = 3. We know that there are the same ranges of 2s from before (that is, the ranges 2000 – 2999, 12000 – 12999, … , 52000 – 52999). How many appear in the 3rd digit in the final, partial range from 62000 – 62523? Well, that should be pretty easy. It’s just 524 (62000, 62001, … , 62523).
if x[d] = 2: count2sinRangeAtDigit(x, d) = 
   Compute y = round down to nearest 10d+1 
   Compute z = right side of x (i.e., x%  10d)
   return y/10 + z + 1**// here why we are doing it ,what is the logic behind this approach** 

上面给出的解释不完全清楚,这就是我在这里问的原因,谢谢

【问题讨论】:

    标签: algorithm math number-theory


    【解决方案1】:

    对我来说,这种解释也很奇怪。另请注意,真正的复杂度是 O(log(n)),因为它取决于数字长度(位数)。

    考虑下一个例子:我们有号码6125

    在第一轮中,我们需要计算从06125 的所有数字中最右边的数字有多少2。我们将数字向下舍入到 61206130。最后一个数字是5&gt;2,所以我们有613 间隔,每个间隔都包含一个数字2 作为最后一个数字 - 这里我们计算最后一个2 的数字,如@9​​87654331@。

    在第二轮中,我们需要计算在从06125 的所有数字中,有多少2作为第二个(右起)数字。我们将数字向下舍入到 61006200。我们还有right=5。数字是2,所以我们有61个区间,每个区间在第二位(20..29, 120..129... 6020..6029)包含十位数字2。我们添加61*10。我们还必须为值 6120..6125

    添加 5+1 2

    在第三轮中,我们需要计算在从06125 的所有数字中,第三个(右起)数字有多少个2。我们将数字向下舍入到 60007000。数字是1,所以我们有6 intervals,每个区间包含一百个数字2在第三位(200.299.. 5200..5299)。所以添加6*100

    我认为现在我们添加1 间隔和thousand of 2 的(2000.2999)作为最左边的数字(6&gt;2)很清楚

    【讨论】:

    • 非常感谢@MBo 为回答我的问题所做的努力,我的一个问题是你是如何找出间隔数的,如果可能的话,你可以在你的答案中添加它吗,请
    • 间隔计数是向下(或向上)四舍五入的数字,除以对应于数字位置的 10 的幂。例如,对于第二个位置,我们有 6100/100 = 61 区间。这意味着 - 我们有 61 百个,每百个包含 10 个数字,在所需位置有数字 2(以 2x 结尾)
    • 链接页面在 Improved Solution 部分包含 C++ 代码。我只是做了另一个解释(在我看来更可靠),代码保持不变。
    猜你喜欢
    • 2022-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-23
    • 2012-08-07
    • 1970-01-01
    • 1970-01-01
    • 2020-05-01
    相关资源
    最近更新 更多