📜  计数最多八位数的N,八进制表示中的位数为D

📅  最后修改于: 2021-04-26 08:25:01             🧑  作者: Mango

给定正整数N和代表数字的整数D ,任务是对范围[1,N]中的数字进行计数,以使该数字的八进制表示形式中的至少一位数字为d

例子:

方法:请按照以下步骤解决问题:

  • [1,N]范围内迭代。对于每个i数字,请检查该数字的八进制表示形式中是否至少有一位数字是d 。如果发现为真,则增加计数。
  • 最后,打印获得的计数。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to count the numbers in given
// range whose octal representation
// contains atleast digit, d
void countNumbers(int n, int d)
{
    // Store count of numbers up to n
    // whose octal representation
    // contains digit d
    int total = 0;
 
    // Iterate over the range[1, n]
    for (int i = 1; i <= n; i++) {
 
        int x = i;
 
        // Calculate digit of i in
        // octal representation
        while (x > 0) {
 
 
            // Check if octal representation
            // of x contains digit d
            if (x % 8 == d) {
 
                // Update total
                total++;
                break;
            }
 
            // Update x
            x = x / 8;
        }
    }
 
 
    // Print the answer
    cout << total;
}
 
 
 
// Driver Code
int main()
{
    // Given N and D
    int n = 20, d = 7;
 
    // Counts and prints numbers
// up to N having D as a digit
// in its octal representation
    countNumbers(n, d);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.io.*;
import java.util.*;
   
class GFG{
  
// Function to count the numbers in given
// range whose octal representation
// contains atleast digit, d
static void countNumbers(int n, int d)
{
     
    // Store count of numbers up to n
    // whose octal representation
    // contains digit d
    int total = 0;
  
    // Iterate over the range[1, n]
    for(int i = 1; i <= n; i++)
    {
        int x = i;
  
        // Calculate digit of i in
        // octal representation
        while (x > 0)
        {
             
            // Check if octal representation
            // of x contains digit d
            if (x % 8 == d)
            {
                 
                // Update total
                total++;
                break;
            }
  
            // Update x
            x = x / 8;
        }
    }
     
    // Print the answer
    System.out.println(total);
}
  
// Driver code
public static void main(String[] args)
{
     
    // Given N and D
    int n = 20, d = 7;
  
    // Counts and prints numbers
    // up to N having D as a digit
    // in its octal representation
    countNumbers(n, d);
}
}
 
// This code is contributed by sanjoy_62


Python3
# Python3 program to implement
# the above approach
 
# Function to count the numbers in given
# range whose octal representation
# contains atleast digit, d
def countNumbers(n, d):
   
    # Store count of numbers up to n
    # whose octal representation
    # contains digit d
    total = 0
 
    # Iterate over the range[1, n]
    for i in range(1, n + 1):
        x = i
 
        # Calculate digit of i in
        # octal representation
        while (x > 0):
 
 
            # Check if octal representation
            # of x contains digit d
            if (x % 8 == d):
 
                # Update total
                total += 1
                break
 
            # Update x
            x = x // 8
 
    # Prthe answer
    print (total)
 
# Driver Code
if __name__ == '__main__':
   
    # Given N and D
    n , d = 20, 7
 
    # Counts and prints numbers
    # up to N having D as a digit
    # in its octal representation
    countNumbers(n, d)
 
# This code is contributed by mohit kumr 29


C#
// C# program to implement
// the above approach
using System;
  
class GFG{
      
// Function to count the numbers in given
// range whose octal representation
// contains atleast digit, d
static void countNumbers(int n, int d)
{
     
    // Store count of numbers up to n
    // whose octal representation
    // contains digit d
    int total = 0;
   
    // Iterate over the range[1, n]
    for(int i = 1; i <= n; i++)
    {
        int x = i;
         
        // Calculate digit of i in
        // octal representation
        while (x > 0)
        {
             
            // Check if octal representation
            // of x contains digit d
            if (x % 8 == d)
            {
                  
                // Update total
                total++;
                break;
            }
   
            // Update x
            x = x / 8;
        }
    }
      
    // Print the answer
    Console.WriteLine(total);
}
  
// Driver Code
static void Main()
{
     
    // Given N and D
    int n = 20, d = 7;
   
    // Counts and prints numbers
    // up to N having D as a digit
    // in its octal representation
    countNumbers(n, d);
}
}
 
// This code is contributed by susmitakundugoaldanga


输出:
2

时间复杂度: O(N * log 8 N)
辅助空间: O(1)