Agregue N dígitos a A de modo que sea divisible por B después de cada suma

Dados tres números enteros A , B y N , repita el siguiente proceso N veces: 
 

  1. Agregue un dígito a A tal que después de agregarlo, A sea divisible por B.
  2. Imprime el valor más pequeño de A posible después de N iteraciones de la operación anterior.
  3. Imprime -1 si la operación falla.

Nota: Necesitamos verificar la divisibilidad después de cada suma de dígitos.
Ejemplos: 
 

Entrada: A = 10, B = 11, N = 1 
Salida: -1 
No importa qué dígito agregue, 10X nunca será divisible por 11.
Entrada: A = 5, B = 3, N = 3 
Salida: 5100 
 

Enfoque: fuerza bruta para que el primer dígito se agregue de 0 a 9 , si ninguno de los dígitos hace que A sea divisible por B , entonces la respuesta es -1 . De lo contrario, agregue el primer dígito que satisfaga la condición y luego agregue 0 después de eso (n-1) veces porque si A es divisible por B , entonces A*10, A*100, … también será divisible por B.
A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
int addNDigits(int a, int b, int n)
{
    int num = a;
 
    // Try all digits from (0 to 9)
    for (int i = 0; i <= 9; i++) {
        int tmp = a * 10 + i;
        if (tmp % b == 0) {
            a = tmp;
            break;
        }
    }
 
    // Fails in the first move itself
    if (num == a)
        return -1;
 
    // Add (n-1) 0's
    for (int j = 0; j < n - 1; j++)
        a *= 10;
 
    return a;
}
 
// Driver Program to test above function
int main()
{
    int a = 5, b = 3, n = 3;
    cout << addNDigits(a, b, n);
    return 0;
}

Java

  //Java implementation of the approach
 
import java.io.*;
 
class GFG {
 
 
static int addNDigits(int a, int b, int n)
{
    int num = a;
 
    // Try all digits from (0 to 9)
    for (int i = 0; i <= 9; i++) {
        int tmp = a * 10 + i;
        if (tmp % b == 0) {
            a = tmp;
            break;
        }
    }
 
    // Fails in the first move itself
    if (num == a)
        return -1;
 
    // Add (n-1) 0's
    for (int j = 0; j < n - 1; j++)
        a *= 10;
 
    return a;
}
 
// Driver Program to test above function
 
    public static void main (String[] args) {
    int a = 5, b = 3, n = 3;
    System.out.print( addNDigits(a, b, n));
    }
}
// This code is contributed by anuj_67..

Python3

# Python3 implementation of the approach
def addNDigits(a, b, n) :
 
    num = a
     
    # Try all digits from (0 to 9)
    for i in range(10) :
        tmp = a * 10 + i
         
        if (tmp % b == 0) :
            a = tmp
            break
         
    # Fails in the first move itself
    if (num == a) :
        return -1
 
    # Add (n-1) 0's
    for j in range(n - 1) :
        a *= 10
 
    return a
 
# Driver Code
if __name__ == "__main__" :
     
    a = 5
    b = 3
    n = 3
 
    print(addNDigits(a, b, n))
 
# This code is contributed by Ryuga

C#

// C# implementation of the approach
using System;
 
class GFG
{
 
static int addNDigits(int a,
                      int b, int n)
{
    int num = a;
 
    // Try all digits from (0 to 9)
    for (int i = 0; i <= 9; i++)
    {
        int tmp = a * 10 + i;
        if (tmp % b == 0)
        {
            a = tmp;
            break;
        }
    }
 
    // Fails in the first move itself
    if (num == a)
        return -1;
 
    // Add (n-1) 0's
    for (int j = 0; j < n - 1; j++)
        a *= 10;
 
    return a;
}
 
// Driver Code
public static void Main ()
{
    int a = 5, b = 3, n = 3;
    Console.WriteLine(addNDigits(a, b, n));
}
}
 
// This code is contributed
// by anuj_67..

PHP

<?php
// PHP implementation of the approach
 
function addNDigits($a, $b, $n)
{
    $num = $a;
 
    // Try all digits from (0 to 9)
    for ($i = 0; $i <= 9; $i++)
    {
        $tmp = $a * 10 + $i;
        if ($tmp % $b == 0)
        {
            $a = $tmp;
            break;
        }
    }
 
    // Fails in the first move itself
    if ($num == $a)
        return -1;
 
    // Add (n-1) 0's
    for ($j = 0; $j < $n - 1; $j++)
        $a *= 10;
 
    return $a;
}
 
// Driver Code
$a = 5; $b = 3; $n = 3;
echo addNDigits($a, $b, $n);
 
// This code is contributed
// by Akanksha Rai

Javascript

<script>
    // Javascript implementation of the approach
     
    function addNDigits(a, b, n)
    {
        let num = a;
 
        // Try all digits from (0 to 9)
        for (let i = 0; i <= 9; i++)
        {
            let tmp = a * 10 + i;
            if (tmp % b == 0)
            {
                a = tmp;
                break;
            }
        }
 
        // Fails in the first move itself
        if (num == a)
            return -1;
 
        // Add (n-1) 0's
        for (let j = 0; j < n - 1; j++)
            a *= 10;
 
        return a;
    }
     
    let a = 5, b = 3, n = 3;
    document.write(addNDigits(a, b, n));
 
</script>
Producción: 

5100

 

Complejidad de tiempo: O(n)

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

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