Dado un número entero N ≥ 1 , la tarea es encontrar el número de N dígitos más pequeño que sea un múltiplo de 5 .
Ejemplos:
Entrada: N = 1
Salida: 5
Entrada: N = 2
Salida: 10
Entrada: N = 3
Salida: 100
Acercarse:
- Si N = 1 , la respuesta será 5 .
- Si N > 1 , la respuesta será (10 (N – 1) ) porque la serie del múltiplo más pequeño de 5 seguirá como 10, 100, 1000, 10000, 100000, …
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the smallest n digit // number which is a multiple of 5 int smallestMultiple(int n) { if (n == 1) return 5; return pow(10, n - 1); } // Driver code int main() { int n = 4; cout << smallestMultiple(n); return 0; }
Java
// Java implementation of the approach class GFG { // Function to return the smallest n digit // number which is a multiple of 5 static int smallestMultiple(int n) { if (n == 1) return 5; return (int)(Math.pow(10, n - 1)); } // Driver code public static void main(String args[]) { int n = 4; System.out.println(smallestMultiple(n)); } }
Python3
# Python3 implementation of the approach # Function to return the smallest n digit # number which is a multiple of 5 def smallestMultiple(n): if (n == 1): return 5 return pow(10, n - 1) # Driver code n = 4 print(smallestMultiple(n))
C#
// C# implementation of the approach using System; class GFG { // Function to return the smallest n digit // number which is a multiple of 5 static int smallestMultiple(int n) { if (n == 1) return 5; return (int)(Math.Pow(10, n - 1)); } // Driver code public static void Main() { int n = 4; Console.Write(smallestMultiple(n)); } }
PHP
<?php // PHP implementation of the approach // Function to return the smallest n digit // number which is a multiple of 5 function smallestMultiple($n) { if ($n == 1) return 5; return pow(10, $n - 1); } // Driver code $n = 4; echo smallestMultiple($n); ?>
Javascript
<script> // Javascript implementation of the approach // Function to return the smallest n digit // number which is a multiple of 5 function smallestMultiple(n) { if (n == 1) return 5; return Math.pow(10, n - 1); } // Driver code var n = 4; document.write(smallestMultiple(n)); // This code is contributed by noob2000. </script>
Producción:
1000
Complejidad de tiempo: O (logn)
Espacio Auxiliar: O(1)