Compruebe si N se puede representar como una suma de números enteros positivos que contengan el dígito D al menos una vez

Dado un entero positivo N y un dígito D , la tarea es comprobar si N puede representarse como una suma de enteros positivos que contengan el dígito D al menos una vez. Si es posible representar N en dicho formato, imprima «Sí» . De lo contrario, escriba “No” .

Ejemplos:

Entrada: N = 24, D = 7
Salida:
Explicación: El valor 24 se puede representar como 17 + 7, ambos con el dígito 7.

Entrada: N = 27 D = 2
Salida:

Enfoque: Siga los pasos para resolver el problema:

A continuación se muestra la implementación del enfoque anterior:

C++

// C++ program for the above approach
 
#include <iostream>
using namespace std;
 
// Function to check if N contains
// digit D in it
bool findDigit(int N, int D)
{
    // Iterate until N is positive
    while (N > 0) {
 
        // Find the last digit
        int a = N % 10;
 
        // If the last digit is the
        // same as digit D
        if (a == D) {
            return true;
        }
 
        N /= 10;
    }
 
    // Return false
    return false;
}
 
// Function to check if the value of
// N can be represented as sum of
// integers having digit d in it
bool check(int N, int D)
{
    // Iterate until N is positive
    while (N > 0) {
 
        // Check if N contains digit
        // D or not
        if (findDigit(N, D) == true) {
            return true;
        }
 
        // Subtracting D from N
        N -= D;
    }
 
    // Return false
    return false;
}
 
// Driver Code
int main()
{
    int N = 24;
    int D = 7;
    if (check(N, D)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
 
    return 0;
}

Java

// Java approach for the above approach
import java.util.*;
 
class GFG{
 
// Function to check if N contains
// digit D in it
static boolean findDigit(int N, int D)
{
     
    // Iterate until N is positive
    while (N > 0)
    {
         
        // Find the last digit
        int a = N % 10;
 
        // If the last digit is the
        // same as digit D
        if (a == D)
        {
            return true;
        }
        N /= 10;
    }
 
    // Return false
    return false;
}
 
// Function to check if the value of
// N can be represented as sum of
// integers having digit d in it
static boolean check(int N, int D)
{
     
    // Iterate until N is positive
    while (N > 0)
    {
         
        // Check if N contains digit
        // D or not
        if (findDigit(N, D) == true)
        {
            return true;
        }
         
        // Subtracting D from N
        N -= D;
    }
     
    // Return false
    return false;
}
  
// Driver Code
public static void main(String[] args)
{
    int N = 24;
    int D = 7;
     
    if (check(N, D))
    {
        System.out.print("Yes");
    }
    else
    {
        System.out.print("No");
    }
}
}
 
// This code is contributed by sanjoy_62

Python3

# Python3 program for the above approach
 
# Function to check if N contains
# digit D in it
def findDigit(N, D):
     
    # Iterate until N is positive
    while (N > 0):
         
        # Find the last digit
        a = N % 10
 
        # If the last digit is the
        # same as digit D
        if (a == D):
            return True
 
        N /= 10
 
    # Return false
    return False
 
# Function to check if the value of
# N can be represented as sum of
# integers having digit d in it
def check(N, D):
     
    # Iterate until N is positive
    while (N > 0):
 
        # Check if N contains digit
        # D or not
        if (findDigit(N, D) == True):
            return True
 
        # Subtracting D from N
        N -= D
 
    # Return false
    return False
 
# Driver Code
if __name__ == '__main__':
     
    N = 24
    D = 7
     
    if (check(N, D)):
        print("Yes")
    else:
        print("No")
 
# This code is contributed by mohit kumar 29

C#

// C# program for the above approach
using System;
  
class GFG{
  
// Function to check if N contains
// digit D in it
static bool findDigit(int N, int D)
{
      
    // Iterate until N is positive
    while (N > 0)
    {
          
        // Find the last digit
        int a = N % 10;
  
        // If the last digit is the
        // same as digit D
        if (a == D)
        {
            return true;
        }
        N /= 10;
    }
  
    // Return false
    return false;
}
  
// Function to check if the value of
// N can be represented as sum of
// integers having digit d in it
static bool check(int N, int D)
{
      
    // Iterate until N is positive
    while (N > 0)
    {
          
        // Check if N contains digit
        // D or not
        if (findDigit(N, D) == true)
        {
            return true;
        }
          
        // Subtracting D from N
        N -= D;
    }
      
    // Return false
    return false;
}
 
  
// Driver Code
public static void Main()
{
    int N = 24;
    int D = 7;
      
    if (check(N, D))
    {
        Console.WriteLine("Yes");
    }
    else
    {
        Console.WriteLine("No");
    }
      
}
}
 
// This code is contributed by code_hunt.

Javascript

<script>
 
// javascript program for the above approach
 
// Function to check if N contains
// digit D in it
function findDigit(N, D)
{
      
    // Iterate until N is positive
    while (N > 0)
    {
          
        // Find the last digit
        let a = N % 10;
  
        // If the last digit is the
        // same as digit D
        if (a == D)
        {
            return true;
        }
        N = Math.floor(N / 10);
    }
  
    // Return false
    return false;
}
  
// Function to check if the value of
// N can be represented as sum of
// integers having digit d in it
function check(N, D)
{
      
    // Iterate until N is positive
    while (N > 0)
    {
          
        // Check if N contains digit
        // D or not
        if (findDigit(N, D) == true)
        {
            return true;
        }
          
        // Subtracting D from N
        N -= D;
    }
      
    // Return false
    return false;
}
 
// Driver Code
 
    let N = 24;
    let D = 7;
      
    if (check(N, D))
    {
        document.write("Yes");
    }
    else
    {
        document.write("No");
    }
</script>
Producción: 

Yes

 

Complejidad de tiempo: O(N)
Complejidad de espacio: O(1)

Publicación traducida automáticamente

Artículo escrito por RohitOberoi y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *