Dada una raíz digital ‘D’ y un número de dígitos ‘K’. La tarea es imprimir un número que contenga K dígitos que tenga su raíz digital igual a D. Imprime ‘-1’ si tal número no existe.
Ejemplos:
Input: D = 4, K = 4 Output: 4000 No. of digits is 4. Sum of digits is also 4. Input: D = 0, K = 1 Output: 0
Enfoque: una observación clave para resolver este problema es que agregar cualquier número de 0 a un número no cambia su raíz digital. Por lo tanto , D seguido de (K-1) 0 es una solución simple.
Caso especial cuando D es 0 y K no es 1 no tiene solución ya que el único número con raíz digital 0 es el 0 mismo.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the above approach #include <bits/stdc++.h> using namespace std; // Function to find a number void printNumberWithDR(int k, int d) { // If d is 0 k has to be 1 if (d == 0 && k != 1) cout << "-1"; else { cout << d; k--; // Print k-1 zeroes while (k--) cout << "0"; } } // Driver code int main() { int k = 4, d = 4; printNumberWithDR(k, d); return 0; }
Java
// Java implementation of the above approach import java.io.*; class GFG { // Function to find a number static void printNumberWithDR(int k, int d) { // If d is 0 k has to be 1 if (d == 0 && k != 1) System.out.print( "-1"); else { System.out.print(d); k--; // Print k-1 zeroes while (k-->0) System.out.print( "0"); } } // Driver code public static void main (String[] args) { int k = 4, d = 4; printNumberWithDR(k, d); } } //This code is contributed by inder_verma..
Python3
# Python3 implementation of # the above approach # Function to find a number def printNumberWithDR( k, d ) : # If d is 0, k has to be 1 if d == 0 and k != 1 : print(-1, end = "") else : print(d, end = "") k -= 1 # Print k-1 zeroes while k : print(0,end = "") k -= 1 # Driver code if __name__ == "__main__" : k, d = 4, 4 # Function call printNumberWithDR( k, d ) # This code is contributed by # ANKITRAI1
C#
// C# implementation of the above approach using System; class GFG { // Function to find a number static void printNumberWithDR(int k, int d) { // If d is 0 k has to be 1 if (d == 0 && k != 1) Console.Write( "-1"); else { Console.Write(d); k--; // Print k-1 zeroes while (k-->0) Console.Write( "0"); } } // Driver code static public void Main () { int k = 4, d = 4; printNumberWithDR(k, d); } } // This code is contributed by ajit.
PHP
<?php // PHP implementation of the above approach // Function to find a number function printNumberWithDR($k, $d) { // If d is 0 k has to be 1 if ($d == 0 && $k != 1) echo "-1"; else { echo $d; $k--; // Print k-1 zeroes while ($k--) echo "0"; } } // Driver code $k = 4; $d = 4; printNumberWithDR($k, $d); // This code is contributed // by akt_mit ?>
Javascript
<script> // Javascript implementation of the above approach // Function to find a number function printNumberWithDR(k, d) { // If d is 0 k has to be 1 if (d == 0 && k != 1) document.write("-1"); else { document.write(d); k--; // Print k-1 zeroes while (k-->0) document.write("0"); } } // Driver Code var k = 4, d = 4; printNumberWithDR(k, d); // This code is contributed by Ankita saini </script>
4000
Complejidad del tiempo: O(K)
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