📜  可被整数n整除的m位整数的计数

📅  最后修改于: 2021-04-27 17:38:46             🧑  作者: Mango

给定两个数字m和n,计数被n整除的m个数字。
例子:

Input : m = 2 
        n = 6
Output : 15
Two digit numbers that are divisible by 6
are 12, 18, 24, 30, 36, ....., 96.

Input : m = 3 
        n = 5
Output :180

一个简单的解决方案是两次尝试所有m位数字。对于每个数字,请检查是否可以将其除以n。如果是,我们增加计数。
一个有效的解决方案包括以下步骤。
该想法基于这样一个事实,即从第一个可除数开始,每个第n个数都可被n除尽。

  1. 查找最大的m位数。
  2. 查找最大的m-1位数字。
  3. 将两个数字均除以n,然后从先前的数中减去。

下面是上述步骤的实现。

C++
// C++ program to count m digit numbers having
// n as divisor.
#include
using namespace std;
 
// Returns count of m digit numbers having n
// as divisor
int findCount(int m, int n)
{   
    // generating largest number of m digit
    int num1 = 0;
    for (int i = 0; i < m; i++)
        num1 = (num1 * 10) + 9;
 
    // generating largest number of m-1 digit
    int num2 = 0;
    for (int i = 0; i < (m - 1); i++)
        num2 = (num2 * 10) + 9;
 
    // returning number of dividend
    return ((num1 / n) - (num2 / n));
}
 
// Driver code
int main()
{
    int m = 2, n = 6;
    printf("%d\n", findCount(m, n));
    return 0;
}


Java
// Java program to count m digit numbers having
// n as divisor.
 
class Main
{
    // Returns count of m digit numbers having n
    // as divisor
    static int findCount(int m, int n)
    {   
        // generating largest number of m digit
        int num1 = 0;
        for (int i = 0; i < m; i++)
            num1 = (num1 * 10) + 9;
      
        // generating largest number of m-1 digit
        int num2 = 0;
        for (int i = 0; i < (m - 1); i++)
            num2 = (num2 * 10) + 9;
      
        // returning number of dividend
        return ((num1 / n) - (num2 / n));
    }
     
    // main function
    public static void main (String[] args)
    {
        int m = 2, n = 6;
        System.out.println(findCount(m, n));
    }
}
 
/* This code is contributed by Harsh Agarwal */


Python3
# Python3 program to count m digit
# numbers having n as divisor.
 
# Returns count of m digit
# numbers having n as divisor
def findCount(m, n):
 
    # Generating largest number of m digit
    num1 = 0
     
    for i in range(0, m):
        num1 = (num1 * 10) + 9
 
    # Generating largest number of m-1 digit
    num2 = 0
     
    for i in range(0, (m - 1)):
        num2 = (num2 * 10) + 9
 
    # returning number of dividend
    return int((num1 / n) - (num2 / n))
 
 
# Driver code
m = 2; n = 6
print(findCount(m, n))
 
# This code is contributed by Smitha Dinesh Semwal


C#
// C# program to count m digit numbers
// having n as divisor.
using System;
 
class GfG {
     
    // Returns count of m digit numbers
    // having n as divisor
    static int findCount(int m, int n)
    {
         
        // generating largest number
        // of m digit
        int num1 = 0;
        for (int i = 0; i < m; i++)
            num1 = (num1 * 10) + 9;
     
        // generating largest number
        // of m-1 digit
        int num2 = 0;
        for (int i = 0; i < (m - 1); i++)
            num2 = (num2 * 10) + 9;
     
        // returning number of dividend
        return ((num1 / n) - (num2 / n));
    }
     
    // main function
    public static void Main ()
    {
        int m = 2, n = 6;
         
        Console.Write(findCount(m, n));
    }
}
 
// This code is contributed by parashar.


PHP


Javascript


输出 :

15