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

📅  最后修改于: 2021-05-13 22:25:40             🧑  作者: 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


输出:
11