📌  相关文章
📜  不包含数字 M 且可被 M 整除的范围内的数字计数。

📅  最后修改于: 2021-09-02 06:02:23             🧑  作者: Mango

给定三个整数,范围下限L 、范围上限U和数字M 。任务是计算 L 和 U 之间的所有数字,使得该数字可以被 M 整除,并且它不包含数字 M。

例子:

Input: M = 9 ,L = 16 , U = 26
Output: 1
Explanation:
Within this given range ,the number that 
follows the above two given conditions is: 18.

Input: M = 6 ,L = 88 , U = 102
Output: 2
Explanation:
Within this given range ,the numbers that 
follows the above two given conditions are: 90 and 102.

方法:

  • 这个想法是从较低的范围(L)迭代到较高的范围(U)并且对于每个数字,
  • 我们将数字的不同数字存储在一个num变量中,并将根据给定的条件检查该集合是否包含数字 M。如果数字不包含给定的数字 M 并且可以被 M 整除,则计数器加 1。
C++
// C++ implementation to illustrate
// the program
#include
using namespace std;
 
// Function to count all the numbers
// which does not contain the digit 'M'
// and is divisible by M
void contain(int L, int U, int M)
{
    int count = 0;
    for(int j = L; j < U; j++)
    {
         
    // Storing all the distinct
    // digits of a number
    set num;
    string str = to_string(j);
    num.insert(str);
         
    // Checking if the two conditions
    // are satisfied or not
    if (j % M == 0 and
        num.find(to_string(M)) == num.end())
    {
        count += 1;
    }
    }
    cout << count - 2;
}
     
// Driver code
int main()
{
    // Lower Range
    int L = 106;
     
    // Upper Range
    int U = 200;
     
    // The digit
    int M = 7;
 
    contain(L, U, M);
}
     
// This code is contributed by BhupendraSingh


Java
// Java implementation to illustrate
// the program
import java.util.*;
 
class GFG{
     
// Function to count all the numbers
// which does not contain the digit 'M'
// and is divisible by M
static void contain(int L, int U, int M)
{
    int count = 0;
    for(int j = L; j < U; j++)
    {
         
        // Storing all the distinct
        // digits of a number
        HashSet num = new HashSet<>();
        String str = Integer.toString(j);
        num.add(str);
 
        // Checking if the two conditions
        // are satisfied or not
        if (j % M == 0 && !num.contains(
            Integer.toString(M)))
        {
            count += 1;
        }
    }
    System.out.println(count - 2);
}
 
// Driver code
public static void main(String[] args)
{
     
    // Lower Range
    int L = 106;
 
    // Upper Range
    int U = 200;
 
    // The digit
    int M = 7;
 
    contain(L, U, M);
}
}
 
// This code is contributed by jrishabh99


Python3
# Python3 implementation to illustrate
# the program
 
# Function to count all the numbers
# which does not contain the digit 'M'
# and is divisible by M
def contain (L,U,M):
    count = 0
    for j in range (L,U+1):
         
        # Storing all the distinct
        # digits of a number
        num = set(str(j))
         
        # Checking if the two conditions
        # are satisfied or not
        if (j % M == 0 and str(M) not in num):
            count += 1
    print (count)
     
#Driver code
if __name__== '__main__':
     
    L = 106 # Lower Range
    U = 200 # Upper Range
    M = 7 # The digit
 
    contain(L,U,M)
 
# This code is contributed by parna_28


C#
// C# implementation to illustrate
// the program
using System;
using System.Collections.Generic;
 
class GFG{
     
// Function to count all the numbers
// which does not contain the digit 'M'
// and is divisible by M
static void contain(int L, int U, int M)
{
    int count = 0;
    for(int j = L; j < U; j++)
    {
         
        // Storing all the distinct
        // digits of a number
        HashSet num = new HashSet();
         
        string str = j.ToString();
        num.Add(str);
 
        // Checking if the two conditions
        // are satisfied or not
        if (j % M == 0 && !num.Contains(M.ToString()))
        {
            count += 1;
        }
    }
    Console.Write(count - 2);
}
 
// Driver code
public static void Main(string[] args)
{
     
    // Lower Range
    int L = 106;
 
    // Upper Range
    int U = 200;
 
    // The digit
    int M = 7;
 
    contain(L, U, M);
}
}
 
// This code is contributed by rutvik_56


Javascript


输出:
11

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live