Recuento mínimo de números requeridos con el dígito unitario X que suma N

Dados dos números enteros N y X , la tarea es encontrar el recuento mínimo de números enteros con suma N y que tengan el dígito unitario X. Si no existe tal representación, imprima -1 .
Ejemplos: 
 

Entrada: N = 38, X = 9 
Salida:
Explicación: 
Se requiere un mínimo de dos números enteros con dígito de unidad como X para representar como una suma igual a 38. 
38 = 19 + 19 o 38 = 29 + 9
Entrada: N = 6, X = 4 
Salida: -1 
Explicación: 
No existe tal representación de 
 

Enfoque: 
siga los pasos a continuación para resolver el problema: 
 

  • Obtener la cifra unitaria de N y comprobar si se consigue mediante la suma de números cuyas cifras unitarias sean X.
  • Si es posible, compruebe si N ? X * (Número mínimo de veces que se debe sumar un número con el dígito unitario X para obtener la suma N ) .
  • Si se cumple la condición anterior, imprima la cantidad mínima de veces que se debe agregar un número con el dígito de la unidad X para obtener la suma N. De lo contrario, imprima -1.

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

C++

// C++ Program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate and return
// the minimum number of times a number
// with unit digit X needs to be added
// to get a sum N
int check(int unit_digit, int X)
{
    int times, digit;
 
    // Calculate the number of
    // additions required to get unit
    // digit of N
    for (int times = 1; times <= 10;
         times++) {
        digit = (X * times) % 10;
        if (digit == unit_digit)
            return times;
    }
 
    // If unit digit of N
    // cannot be obtained
    return -1;
}
 
// Function to return the minimum
// number required to represent N
int getNum(int N, int X)
{
    int unit_digit;
 
    // Stores unit digit of N
    unit_digit = N % 10;
 
    // Stores minimum addition
    // of X required to
    // obtain unit digit of N
    int times = check(unit_digit, X);
 
    // If unit digit of N
    // cannot be obtained
    if (times == -1)
        return times;
 
    // Otherwise
    else {
 
        // If N is greater than
        // or equal to (X*times)
        if (N >= (times * X))
 
            // Minimum count of numbers
            // that needed to represent N
            return times;
 
        // Representation not
        // possible
        else
            return -1;
    }
}
 
// Driver Code
int main()
{
    int N = 58, X = 7;
    cout << getNum(N, X) << endl;
    return 0;
}

Java

// Java Program to implement
// the above approach
class GFG{
     
// Function to calculate and return
// the minimum number of times a number
// with unit digit X needs to be added
// to get a sum N
static int check(int unit_digit, int X)
{
    int times, digit;
 
    // Calculate the number of
    // additions required to get unit
    // digit of N
    for (times = 1; times <= 10;
                    times++)
    {
        digit = (X * times) % 10;
        if (digit == unit_digit)
            return times;
    }
 
    // If unit digit of N
    // cannot be obtained
    return -1;
}
 
// Function to return the minimum
// number required to represent N
static int getNum(int N, int X)
{
    int unit_digit;
 
    // Stores unit digit of N
    unit_digit = N % 10;
 
    // Stores minimum addition
    // of X required to
    // obtain unit digit of N
    int times = check(unit_digit, X);
 
    // If unit digit of N
    // cannot be obtained
    if (times == -1)
        return times;
 
    // Otherwise
    else
    {
 
        // If N is greater than
        // or equal to (X*times)
        if (N >= (times * X))
 
            // Minimum count of numbers
            // that needed to represent N
            return times;
 
        // Representation not
        // possible
        else
            return -1;
    }
}
 
// Driver Code
public static void main(String []args)
{
    int N = 58, X = 7;
    System.out.println( getNum(N, X));
}
}
 
// This code is contributed by Ritik Bansal

Python3

# Python3 program to implement
# the above approach
 
# Function to calculate and return
# the minimum number of times a number
# with unit digit X needs to be added
# to get a sum N
def check(unit_digit, X):
     
    # Calculate the number of additions
    # required to get unit digit of N
    for times in range(1, 11):
        digit = (X * times) % 10
        if (digit == unit_digit):
            return times
             
    # If unit digit of N
    # cannot be obtained
    return -1
 
# Function to return the minimum
# number required to represent N
def getNum(N, X):
     
    # Stores unit digit of N
    unit_digit = N % 10
 
    # Stores minimum addition
    # of X required to
    # obtain unit digit of N
    times = check(unit_digit, X)
 
    # If unit digit of N
    # cannot be obtained
    if (times == -1):
        return times
 
    # Otherwise
    else:
 
        # If N is greater than
        # or equal to (X*times)
        if (N >= (times * X)):
 
            # Minimum count of numbers
            # that needed to represent N
            return times
 
        # Representation not
        # possible
        else:
            return -1
 
# Driver Code
N = 58
X = 7
 
print(getNum(N, X))
 
# This code is contributed by Sanjit_Prasad

C#

// C# Program to implement
// the above approach
using System;
class GFG{
     
// Function to calculate and return
// the minimum number of times a number
// with unit digit X needs to be added
// to get a sum N
static int check(int unit_digit, int X)
{
    int times, digit;
 
    // Calculate the number of
    // additions required to get unit
    // digit of N
    for (times = 1; times <= 10;
                    times++)
    {
        digit = (X * times) % 10;
        if (digit == unit_digit)
            return times;
    }
 
    // If unit digit of N
    // cannot be obtained
    return -1;
}
 
// Function to return the minimum
// number required to represent N
static int getNum(int N, int X)
{
    int unit_digit;
 
    // Stores unit digit of N
    unit_digit = N % 10;
 
    // Stores minimum addition
    // of X required to
    // obtain unit digit of N
    int times = check(unit_digit, X);
 
    // If unit digit of N
    // cannot be obtained
    if (times == -1)
        return times;
 
    // Otherwise
    else
    {
 
        // If N is greater than
        // or equal to (X*times)
        if (N >= (times * X))
 
            // Minimum count of numbers
            // that needed to represent N
            return times;
 
        // Representation not
        // possible
        else
            return -1;
    }
}
 
// Driver Code
public static void Main()
{
    int N = 58, X = 7;
    Console.Write(getNum(N, X));
}
}
 
// This code is contributed by Code_Mech

Javascript

<script>
 
// Javascript program implementation
// of the approach
 
// Function to calculate and return
// the minimum number of times a number
// with unit digit X needs to be added
// to get a sum N
function check(unit_digit, X)
{
    let times, digit;
   
    // Calculate the number of
    // additions required to get unit
    // digit of N
    for (times = 1; times <= 10;
                    times++)
    {
        digit = (X * times) % 10;
        if (digit == unit_digit)
            return times;
    }
   
    // If unit digit of N
    // cannot be obtained
    return -1;
}
   
// Function to return the minimum
// number required to represent N
function getNum(N, X)
{
    let unit_digit;
   
    // Stores unit digit of N
    unit_digit = N % 10;
   
    // Stores minimum addition
    // of X required to
    // obtain unit digit of N
    let times = check(unit_digit, X);
   
    // If unit digit of N
    // cannot be obtained
    if (times == -1)
        return times;
   
    // Otherwise
    else
    {
   
        // If N is greater than
        // or equal to (X*times)
        if (N >= (times * X))
   
            // Minimum count of numbers
            // that needed to represent N
            return times;
   
        // Representation not
        // possible
        else
            return -1;
    }
}
  
// Driver Code
     
    let N = 58, X = 7;
    document.write( getNum(N, X));
    
   // This code is contributed by splevel62.
</script>
Producción: 

4

 

Tiempo Complejidad: O(1) 
Espacio Auxiliar: O(1)
 

Publicación traducida automáticamente

Artículo escrito por adarsh289 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 *