📜  在[0..n]范围内查找数字d的出现

📅  最后修改于: 2021-04-27 05:36:31             🧑  作者: Mango

给定数字n和数字d,计算所有出现的d,范围从0到n。

例子:

Input :  n = 25 
         d = 2
Output : 9
The occurrences are 2, 12, 20, 21
22 (Two occurrences), 23, 24, 25

Input : n = 25 
        d = 3
Output :3
The occurrences are 3, 13, 23

Input : n = 32 
        d = 3
Output : 6
The occurrences are 3, 13, 23, 30, 31, 32

d的首次出现不能在数字d之前。因此,我们从d开始进行迭代,并一次又一次地进行以下检查。除了在步骤2.a和2.b中提到的情况外,我们大多数时候(在步骤2中)将数字跳了10次。
步骤1 :检查数字的最后一位是否等于d,如果最后一位则增加计数。
第二步
a)如果数字可以被10整除,则同时增加计数和数字(例如,如果数字30可以被10整除并且d = 3,那么我们必须对31-39之间的所有数字进行计数,这就是为什么我们将count加1并将number加1)
b)否则,如果数字的第一个数字比d小一个,则意味着我们来到了必须将数字增加10并从中减去d的行。例如,如果我们在d = 3时达到23,则将数字增加23 + 10-3 = 30)
c)否则仅将数字增加10。 (例如:d = 3,itr = 3,然后增加10 ie13,23)
步骤3:-返回计数。

C++
// C++ program to count appearances of
// a digit 'd' in range from [0..n]
#include 
using namespace std;
  
int getOccurence(int n, int d)
{
    int result = 0; // Initialize result
  
    // Count appearances in numbers starting
    // from d.
    int itr = d;
    while (itr <= n)
    {
        // When the last digit is equal to d
        if (itr%10 == d)
            result++;
  
        // When the first digit is equal to d then
        if (itr != 0 && itr/10 == d)
        {
            // increment result as well as number
            result++;
            itr++;
        }
  
        // In case of reverse of number such as 12 and 21
        else if (itr/10 == d-1)
            itr = itr + (10 - d);
        else
            itr = itr+10;
    }
    return result;
}
  
// Driver code
int main(void)
{
    int n = 11, d = 1;
    cout << getOccurence(n, d);
    return 0;
}


Java
// java program to count appearances of
// a digit 'd' in range from [0..n]
import java.*;
  
public class GFG {
      
    static int getOccurence(int n, int d)
    {
          
        // Initialize result
        int result = 0;
      
        // Count appearances in numbers
        // starting from d.
        int itr = d;
          
        while (itr <= n)
        {
              
            // When the last digit is
            // equal to d
            if (itr % 10 == d)
                result++;
      
            // When the first digit is
            // equal to d then
            if (itr != 0 && itr/10 == d)
            {
                  
                // increment result as
                // well as number
                result++;
                itr++;
            }
      
            // In case of reverse of number 
            // such as 12 and 21
            else if (itr/10 == d-1)
                itr = itr + (10 - d);
            else
                itr = itr + 10;
        }
          
        return result;
    }
  
      
    // Driver code
    public static void main (String[] args)
    {
        int n = 11, d = 1;
      
        System.out.println(getOccurence(n, d) );
    }
}
  
// This code is contributed by Sam007.


Python3
# Python3 program to count appearances 
# of a digit 'd' in range from [0..n] 
import math;
def getOccurence(n, d): 
      
    # Initialize result 
    result = 0; 
  
    # Count appearances in numbers 
    # starting from d. 
    itr = d; 
    while(itr <= n): 
          
        # When the last digit is equal to d 
        if (itr % 10 == d): 
            result += 1; 
  
        # When the first digit is equal to d then 
        if (itr != 0 and math.floor(itr / 10) == d):
              
            # increment result as well as number 
            result += 1; 
            itr += 1; 
  
        # In case of reverse of number 
        # such as 12 and 21 
        elif (math.floor(itr / 10) == d - 1): 
            itr = itr + (10 - d); 
        else:
            itr = itr + 10; 
  
    return result; 
  
# Driver code 
n = 11; 
d = 1; 
print(getOccurence(n, d)); 
  
# This code is contributed by mits


C#
// C# program to count appearances of
// a digit 'd' in range from [0..n]
using System;
          
public class GFG {
      
    static int getOccurence(int n, int d)
    {
          
        // Initialize result
        int result = 0;
      
        // Count appearances in numbers
        // starting from d.
        int itr = d;
        while (itr <= n)
        {
              
            // When the last digit is
            // equal to d
            if (itr % 10 == d)
                result++;
      
            // When the first digit is
            // equal to d then
            if (itr != 0 && itr/10 == d)
            {
                  
                // increment result as 
                // well as number
                result++;
                itr++;
            }
      
            // In case of reverse of number
            // such as 12 and 21
            else if (itr/10 == d-1)
                itr = itr + (10 - d);
            else
                itr = itr + 10;
        }
          
        return result;
    }
      
    // Driver code
    public static void Main() 
    {
        int n = 11, d = 1;
          
        Console.Write(getOccurence(n, d));
    }
}
  
// This code is contributed by Sam007.


PHP



输出:

4