Dado un entero positivo N y base B , la tarea es encontrar los números de N dígitos más grandes de Base B en forma decimal.
Ejemplos:
Entrada: N = 2, B = 10
Salida: 99Entrada: N = 2, B = 5
Salida: 24
Enfoque:
dado que hay B dígitos en base B, con estos dígitos podemos crear B N strings de longitud N. Representan los números enteros en el rango de 0 a B N – 1
Por lo tanto, el mayor número de N dígitos de base B en decimal la forma está dada por B N – 1 .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the approach #include <bits/stdc++.h> using namespace std; // Function to print the largest // N-digit numbers of base b void findNumbers(int n, int b) { // Find the largest N digit // number in base b using the // formula B^N - 1 int largest = pow(b, n) - 1; // Print the largest number cout << largest << endl; } // Driver Code int main() { // Given Number and Base int N = 2, B = 5; // Function Call findNumbers(N, B); return 0; }
Java
// Java program for the approach import java.util.*; class GFG{ // Function to print the largest // N-digit numbers of base b static void findNumbers(int n, int b) { // Find the largest N digit // number in base b using the // formula B^N - 1 double largest = Math.pow(b, n) - 1; // Print the largest number System.out.println(largest); } // Driver Code public static void main(String []args) { // Given Number and Base int N = 2, B = 5; // Function Call findNumbers(N, B); } } // This code is contributed by Ritik Bansal
Python3
# Python3 program for the above approach # Function to print the largest # N-digit numbers of base b def findNumbers(n, b): # Find the largest N digit # number in base b using the # formula B^N - 1 largest = pow(b, n) - 1 # Print the largest number print(largest) # Driver Code # Given number and base N, B = 2, 5 # Function Call findNumbers(N, B) # This code is contributed by jrishabh99
C#
// C# program for the approach using System; class GFG{ // Function to print the largest // N-digit numbers of base b static void findNumbers(int n, int b) { // Find the largest N digit // number in base b using the // formula B^N - 1 double largest = Math.Pow(b, n) - 1; // Print the largest number Console.Write(largest); } // Driver Code public static void Main(String []args) { // Given Number and Base int N = 2, B = 5; // Function Call findNumbers(N, B); } } // This code is contributed by shivanisinghss2110
Javascript
<script> // javascript program for the approach // Function to print the largest // N-digit numbers of base b function findNumbers( n, b) { // Find the largest N digit // number in base b using the // formula B^N - 1 let largest = Math.pow(b, n) - 1; // Print the largest number document.write(largest); } // Driver Code // Given Number and Base let N = 2, B = 5; // Function Call findNumbers(N, B); // This code is contributed by aashish1995 </script>
Producción:
24
Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)