📌  相关文章
📜  以0作为数字和最大’d’数字对正整数进行计数

📅  最后修改于: 2021-04-29 06:25:23             🧑  作者: Mango

给定一个数字d,代表一个数字的位数。求出正整数的总数,该整数中至少有一个零且包含d或更少的数字。

Examples:
Input : d = 1
Output : 0
There's no natural number of 1 digit that contains a zero.

Input : d = 2
Output : 9

Input : d = 3
Output : 180
For d = 3, we've to count numbers from 1 to 999, that have 
atleast one zero in them.
Similarly for d=4, we'd check every number from 1 to 9999. 

我们强烈建议您单击此处并进行实践,然后再继续解决方案。

这主要是下面帖子的扩展。
用0作为数字计算’d’位正整数。
如果我们仔细观察,该问题与我们在第一组中讨论的问题非常相似。对于给定的d,如果找到具有0且由数字1,2,3…..,d组成的数字,则可以得到所需的答案。最后,我们可以添加它们以获取输出。
以下是相同的程序。

C++
// C++ program to find the count of positive integer of a
// given number of digits that contain atleast one zero
#include
using namespace std;
 
// Returns count of 'd' digit integers have 0 as a digit
int findCount(int d)
{
    return 9*(pow(10,d-1) - pow(9,d-1));
}
 
// utility function to count the required answer
int findCountUpto(int d)
{
    // Count of numbers with digits smaller than
    // or equal to d.
    int totalCount = 0;
    for (int i=1; i<=d; i++)
        totalCount += findCount(i);
 
    return totalCount;
}
 
// Driver Code
int main()
{
    int d = 1;
    cout << findCountUpto(d) << endl;
 
    d = 2;
    cout << findCountUpto(d) << endl;
 
    d = 4;
    cout << findCountUpto(d) << endl;
    return 0;
}


Java
// Java program to find the count of
// positive integer of agiven number
// of digits that contain atleast one zero
import java.io.*;
import java.math.*;
 
class GFG {
     
    // Returns count of 'd' digit
    // integers have 0 as a digit
    static int findCount(int d)
    {
        return 9 * (int)((Math.pow(10, d - 1)
                         - Math.pow(9, d - 1)));
    }
     
    // utility function to count
    // the required answer
    static int findCountUpto(int d)
    {
        // Count of numbers with digits
        // smaller than or equal to d.
        int totalCount = 0;
        for (int i = 1; i <= d; i++)
            totalCount += findCount(i);
     
        return totalCount;
    }
     
    // Driver Code
    public static void main(String args[])
    {
        int d = 1;
        System.out.println(findCountUpto(d));
     
        d = 2;
        System.out.println( findCountUpto(d) );
     
        d = 4;
        System.out.println(findCountUpto(d));
    }
}
 
 
/*This code is contributed by Nikita Tiwari.*/


Python3
# Python 3 program to find the
# count of natural numbers upto a
# given number of digits that
# contain atleast one zero
import math
 
 
# Utility function to calculate
# the count of natural numbers
# upto a given number of digits
# that contain atleast one zero
def findCountUpto(d) :
    # Sum of two GP series
    GP1_Sum = 9*((int)((math.pow(10,d))-1)//9)
    GP2_Sum = 9*((int)((math.pow(9,d))-1)//8)
  
    return GP1_Sum - GP2_Sum
 
 
# Driver Code
d = 1
print(findCountUpto(d))
 
d = 2
print(findCountUpto(d))
 
d = 4
print(findCountUpto(d))
 
 
# This code is contributed by Nikita Tiwari.


C#
// C# program to find the count of
// positive integer of agiven number
// of digits that contain atleast
// one zero
using System;
 
class GFG {
     
    // Returns count of 'd' digit
    // integers have 0 as a digit
    static int findCount(int d)
    {
        return 9 * (int)((Math.Pow(10, d - 1)
                        - Math.Pow(9, d - 1)));
    }
     
    // utility function to count
    // the required answer
    static int findCountUpto(int d)
    {
        // Count of numbers with digits
        // smaller than or equal to d.
        int totalCount = 0;
        for (int i = 1; i <= d; i++)
            totalCount += findCount(i);
     
        return totalCount;
    }
     
    // Driver Code
    public static void Main()
    {
        int d = 1;
        Console.WriteLine(findCountUpto(d));
     
        d = 2;
        Console.WriteLine( findCountUpto(d) );
     
        d = 4;
        Console.WriteLine(findCountUpto(d));
    }
}
 
// This code is contributed by Sam007


PHP


Javascript


C++
// C++ program to find the count of natural numbers upto a
// given number of digits that contain atleast one zero
#include
using namespace std;
 
// Utility function to calculate the count of natural numbers
// upto a given number of digits that contain atleast one zero
int findCountUpto(int d)
{
    // Sum of two GP series
    int GP1_Sum = 9*((pow(10,d)-1)/9);
    int GP2_Sum = 9*((pow(9,d)-1)/8);
 
    return GP1_Sum - GP2_Sum;
}
 
// Driver Code
int main()
{
    int d = 1;
    cout << findCountUpto(d) << endl;
 
    d = 2;
    cout << findCountUpto(d) << endl;
 
    d = 4;
    cout << findCountUpto(d) << endl;
    return 0;
}


Java
// Java program to find the count
// of natural numbers upto a
// given number of digits
// that contain atleast one zero
import java.io.*;
import java.math.*;
 
class GFG {
     
    // Utility function to calculate
    // the count of natural numbers
    // upto a given number of digits
    // that contain atleast one zero
    static int findCountUpto(int d)
    {
        // Sum of two GP series
        int GP1_Sum = 9 * ((int)((Math.pow(10, d)) - 1) / 9);
        int GP2_Sum = 9 * ((int)((Math.pow(9, d)) - 1) / 8);
     
        return GP1_Sum - GP2_Sum;
    }
     
    // Driver Code
    public static void main(String args[])
    {
        int d = 1;
        System.out.println(findCountUpto(d));
         
        d = 2;
        System.out.println(findCountUpto(d));
         
        d = 4;
        System.out.println(findCountUpto(d));
         
    }
}
 
 
/* This code is contributed by Nikita Tiwari.*/


Python3
# Python 3 program to find the
# count of positive integer of a
# given number of digits that
# contain atleast one zero
import math
 
# Returns count of 'd' digit
# integers have 0 as a digit
def findCount(d) :
    return 9*(pow(10,d-1) - pow(9,d-1));
 
 
# utility function to count
# the required answer
def findCountUpto(d) :
 
    # Count of numbers with
    # digits smaller than
    # or equal to d.
    totalCount = 0
    for i in range(1,d+1) :
        totalCount = totalCount + findCount(i)
  
    return totalCount
 
  
# Driver Code
d = 1
print(findCountUpto(d))
 
d = 2
print(findCountUpto(d))
 
d = 4
print(findCountUpto(d))
 
 
# This code is contributed by Nikita Tiwari.


C#
// C# program to find the count
// of natural numbers upto a
// given number of digits
// that contain atleast one zero
using System;
 
class GFG {
     
    // Utility function to calculate
    // the count of natural numbers
    // upto a given number of digits
    // that contain atleast one zero
    static int findCountUpto(int d)
    {
         
        // Sum of two GP series
        int GP1_Sum = 9 * ((int)((Math.Pow(10,
                                d)) - 1) / 9);
        int GP2_Sum = 9 * ((int)((Math.Pow(9,
                                d)) - 1) / 8);
     
        return GP1_Sum - GP2_Sum;
    }
     
    // Driver Code
    public static void Main()
    {
        int d = 1;
        Console.WriteLine(findCountUpto(d));
         
        d = 2;
        Console.WriteLine(findCountUpto(d));
         
        d = 4;
        Console.WriteLine(findCountUpto(d));
         
    }
}
 
// This code is contributed by Sam007


PHP


输出 :

0
9
2619 

时间复杂度:O(d)
辅助空间:O(1)
我们可以使解决方案更有效吗?
是的,如果我们仔细观察,所需的答案是使用以下两个几何级数之和得出的:

GP 1的第i项= 9 * 10 i -1其中1 <= i <= d GP 2的第i项= 9 * 9 i -1其中1 <= i <= d , Answer=\sum_{k=1}^{d}(G.P1)-\sum_{k=1}^{d}(G.P2) GP 1的总和= 9 *(10 d -1)/(10-1)= 9 *(10 d -1)/ 9类似地,GP 2的总和= 9 *(9 d -1)/(9-1 )= 9 *(9 d -1)/ 8使用以上事实,我们可以优化解决方案以在O(1)中运行

下面是一个有效的程序。

C++

// C++ program to find the count of natural numbers upto a
// given number of digits that contain atleast one zero
#include
using namespace std;
 
// Utility function to calculate the count of natural numbers
// upto a given number of digits that contain atleast one zero
int findCountUpto(int d)
{
    // Sum of two GP series
    int GP1_Sum = 9*((pow(10,d)-1)/9);
    int GP2_Sum = 9*((pow(9,d)-1)/8);
 
    return GP1_Sum - GP2_Sum;
}
 
// Driver Code
int main()
{
    int d = 1;
    cout << findCountUpto(d) << endl;
 
    d = 2;
    cout << findCountUpto(d) << endl;
 
    d = 4;
    cout << findCountUpto(d) << endl;
    return 0;
}

Java

// Java program to find the count
// of natural numbers upto a
// given number of digits
// that contain atleast one zero
import java.io.*;
import java.math.*;
 
class GFG {
     
    // Utility function to calculate
    // the count of natural numbers
    // upto a given number of digits
    // that contain atleast one zero
    static int findCountUpto(int d)
    {
        // Sum of two GP series
        int GP1_Sum = 9 * ((int)((Math.pow(10, d)) - 1) / 9);
        int GP2_Sum = 9 * ((int)((Math.pow(9, d)) - 1) / 8);
     
        return GP1_Sum - GP2_Sum;
    }
     
    // Driver Code
    public static void main(String args[])
    {
        int d = 1;
        System.out.println(findCountUpto(d));
         
        d = 2;
        System.out.println(findCountUpto(d));
         
        d = 4;
        System.out.println(findCountUpto(d));
         
    }
}
 
 
/* This code is contributed by Nikita Tiwari.*/

Python3

# Python 3 program to find the
# count of positive integer of a
# given number of digits that
# contain atleast one zero
import math
 
# Returns count of 'd' digit
# integers have 0 as a digit
def findCount(d) :
    return 9*(pow(10,d-1) - pow(9,d-1));
 
 
# utility function to count
# the required answer
def findCountUpto(d) :
 
    # Count of numbers with
    # digits smaller than
    # or equal to d.
    totalCount = 0
    for i in range(1,d+1) :
        totalCount = totalCount + findCount(i)
  
    return totalCount
 
  
# Driver Code
d = 1
print(findCountUpto(d))
 
d = 2
print(findCountUpto(d))
 
d = 4
print(findCountUpto(d))
 
 
# This code is contributed by Nikita Tiwari.

C#

// C# program to find the count
// of natural numbers upto a
// given number of digits
// that contain atleast one zero
using System;
 
class GFG {
     
    // Utility function to calculate
    // the count of natural numbers
    // upto a given number of digits
    // that contain atleast one zero
    static int findCountUpto(int d)
    {
         
        // Sum of two GP series
        int GP1_Sum = 9 * ((int)((Math.Pow(10,
                                d)) - 1) / 9);
        int GP2_Sum = 9 * ((int)((Math.Pow(9,
                                d)) - 1) / 8);
     
        return GP1_Sum - GP2_Sum;
    }
     
    // Driver Code
    public static void Main()
    {
        int d = 1;
        Console.WriteLine(findCountUpto(d));
         
        d = 2;
        Console.WriteLine(findCountUpto(d));
         
        d = 4;
        Console.WriteLine(findCountUpto(d));
         
    }
}
 
// This code is contributed by Sam007

的PHP


输出 :

0
9
2619 

时间复杂度:O(1)
辅助空间:O(1)
在下一组中,我们将看到另一个难度增加的问题,可以使用非常相似的技术来解决。