Dados dos enteros N y K , la tarea es imprimir todos los números del rango [1, N] cuyo producto de dígitos sea igual a K . Si no se encuentra dicho número, imprima “-1” .
Ejemplos:
Entrada: N = 100, K = 25
Salida: 55
Explicación: Solo hay un solo número 55 cuyo producto de dígitos es igual a K.Entrada: N = 500, K = 10
Salida: 25 52 125 152 215 251
Enfoque: siga los pasos a continuación para resolver el problema:
- Inicialice una variable, digamos una bandera, para almacenar si existe o no algún número que satisfaga las condiciones dadas.
- Declare una función, productOfDigits(), para encontrar el producto de los dígitos del número.
- Iterar sobre el rango [1, N] :
- Si el producto de los dígitos de arr[i] es igual a K , imprima ese número y establezca flag = 1 .
- Si flag es igual a 0 , significa que no se encuentra ese número en el rango [1, N] . Por lo tanto, imprima “-1” .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the product // of digits of a number int productOfDigits(int N) { // Stores the product of // digits of a number int product = 1; while (N != 0) { product = product * (N % 10); N = N / 10; } // Return the product return product; } // Function to print all numbers upto // N having product of digits equal to K void productOfDigitsK(int N, int K) { // Stores whether any number satisfying // the given conditions exists or not int flag = 0; // Iterate over the range [1, N] for (int i = 1; i <= N; ++i) { // If product of digits of // arr[i] is equal to K or not if (K == productOfDigits(i)) { // Print that number cout << i << " "; flag = 1; } } // If no numbers are found if (flag == 0) cout << "-1"; } // Driver Code int main() { // Given value of N & K int N = 500, K = 10; // Function call to print all numbers // from [1, N] with product of digits K productOfDigitsK(N, K); }
Java
// Java Program to implement // the above approach import java.io.*; import java.util.*; class GFG { // Function to find the product // of digits of a number static int productOfDigits(int N) { // Stores the product of // digits of a number int product = 1; while (N != 0) { product = product * (N % 10); N = N / 10; } // Return the product return product; } // Function to print all numbers upto // N having product of digits equal to K static void productOfDigitsK(int N, int K) { // Stores whether any number satisfying // the given conditions exists or not int flag = 0; // Iterate over the range [1, N] for (int i = 1; i <= N; ++i) { // If product of digits of // arr[i] is equal to K or not if (K == productOfDigits(i)) { // Print that number System.out.print(i + " "); flag = 1; } } // If no numbers are found if (flag == 0) System.out.println(-1); } // Driver Code public static void main(String[] args) { // Given value of N & K int N = 500, K = 10; // Function call to print all numbers // from [1, N] with product of digits K productOfDigitsK(N, K); } } // This code is contribute by Kingash.
Python3
# Python3 program for the above approach # Function to find the product # of digits of a number def productOfDigits(N) : # Stores the product of # digits of a number product = 1; while (N != 0) : product = product * (N % 10); N = N // 10; # Return the product return product; # Function to print all numbers upto # N having product of digits equal to K def productOfDigitsK(N, K) : # Stores whether any number satisfying # the given conditions exists or not flag = 0; # Iterate over the range [1, N] for i in range(1, N + 1) : # If product of digits of # arr[i] is equal to K or not if (K == productOfDigits(i)) : # Print that number print(i, end =" "); flag = 1; # If no numbers are found if (flag == 0) : print("-1"); # Driver Code if __name__ == "__main__" : # Given value of N & K N = 500; K = 10; # Function call to print all numbers # from [1, N] with product of digits K productOfDigitsK(N, K); # This code is contributed by AnkThon
C#
// C# program for the above approach using System; class GFG { // Function to find the product // of digits of a number static int productOfDigits(int N) { // Stores the product of // digits of a number int product = 1; while (N != 0) { product = product * (N % 10); N = N / 10; } // Return the product return product; } // Function to print all numbers upto // N having product of digits equal to K static void productOfDigitsK(int N, int K) { // Stores whether any number satisfying // the given conditions exists or not int flag = 0; // Iterate over the range [1, N] for (int i = 1; i <= N; ++i) { // If product of digits of // arr[i] is equal to K or not if (K == productOfDigits(i)) { // Print that number Console.Write(i + " "); flag = 1; } } // If no numbers are found if (flag == 0) Console.WriteLine(-1); } // Driver Code static public void Main() { // Given value of N & K int N = 500, K = 10; // Function call to print all numbers // from [1, N] with product of digits K productOfDigitsK(N, K); } } // This code is contributed by jana_sayantan.
Javascript
<script> // Javascript program for the above approach // Function to find the product // of digits of a number function productOfDigits(N) { // Stores the product of // digits of a number let product = 1; while (N != 0) { product = product * (N % 10); N = Math.floor(N / 10); } // Return the product return product; } // Function to print all numbers upto // N having product of digits equal to K function productOfDigitsK(N, K) { // Stores whether any number satisfying // the given conditions exists or not let flag = 0; // Iterate over the range [1, N] for (let i = 1; i <= N; ++i) { // If product of digits of // arr[i] is equal to K or not if (K == productOfDigits(i)) { // Print that number document.write(i + " "); flag = 1; } } // If no numbers are found if (flag == 0) document.write(-1); } // Driver Code // Given value of N & K let N = 500, K = 10; // Function call to print all numbers // from [1, N] with product of digits K productOfDigitsK(N, K); </script>
Producción
25 52 125 152 215 251
Complejidad de tiempo: O(N * logN)
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por patelajeet y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA