Dado un número entero N , la tarea es encontrar los números de N dígitos más pequeños y más grandes en el sistema de numeración octal .
Ejemplos:
Entrada: N = 4
Salida:
Mayor: 7777
Menor: 1000Entrada: N = 2
Salida:
Mayor: 77
Menor: 10
Enfoque: Se pueden seguir los siguientes pasos para calcular la respuesta requerida:
- Número más grande: para obtener el número más grande, cada dígito del número debe ser máximo. El dígito máximo en el sistema numérico Octal es ‘ 7 ‘. Por lo tanto:
1 Digit Largest Number: '7' 2 Digit Largest Number: '77' 3 Digit Largest Number: '777' . . . N Digit Largest Number: '777....(N) times'
- Número más pequeño: el número más pequeño en el número octal es ‘ 0 ‘. La idea es que el primer dígito debe ser el mínimo posible distinto de 0, que es ‘1’, y los dígitos restantes deben ser 0 . Por lo tanto:
1 Digit Smallest Number: '1' 2 Digit Smallest Number: '10' 3 Digit Smallest Number: '100' . . . N Digit Smallest Number: '100....(N - 1) times'
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to find the largest // and smallest N-digit numbers // in Octal Number System #include <bits/stdc++.h> using namespace std; // Function to return the largest // N-digit number in Octal // Number System string findLargest(int N) { // Append '7' N times string largest = string(N, '7'); return largest; } // Function to return the smallest // N-digit number in Octal // Number System string findSmallest(int N) { // Append '0' (N - 1) times to 1 string smallest = "1" + string((N - 1), '0'); return smallest; } // Function to print the largest and // smallest N-digit Octal number void printLargestSmallest(int N) { cout << "Largest: " << findLargest(N) << endl; cout << "Smallest: " << findSmallest(N) << endl; } // Driver code int main() { int N = 4; // Function Call printLargestSmallest(N); return 0; }
Java
// Java program to find the largest // and smallest N-digit numbers // in Octal Number System class GFG { // Function to return the largest // N-digit number in Octal // Number System static String findLargest(int N) { // Append '7' N times String largest = strings(N, '7'); return largest; } // Function to return the smallest // N-digit number in Octal // Number System static String findSmallest(int N) { // Append '0' (N - 1) times to 1 String smallest = "1" + strings((N - 1), '0'); return smallest; } private static String strings(int N, char c) { String temp =""; for(int i= 0; i < N; i++) { temp+=c; } return temp; } // Function to print the largest and // smallest N-digit Octal number static void printLargestSmallest(int N) { System.out.print("Largest: " + findLargest(N) +"\n"); System.out.print("Smallest: " + findSmallest(N) +"\n"); } // Driver code public static void main(String[] args) { int N = 4; // Function Call printLargestSmallest(N); } } // This code is contributed by 29AjayKumar
Python3
# Python program to find the largest # and smallest N-digit numbers # in Octal Number System # Function to return the largest # N-digit number in Octal # Number System def findLargest(N): # Append '7' N times largest = strings(N, '7'); return largest; # Function to return the smallest # N-digit number in Octal # Number System def findSmallest(N): # Append '0' (N - 1) times to 1 smallest = "1" + strings((N - 1), '0'); return smallest; def strings(N, c): temp = ""; for i in range(N): temp += c; return temp; # Function to print the largest and # smallest N-digit Octal number def printLargestSmallest(N): print("Largest: ",findLargest(N)); print("Smallest: ",findSmallest(N)); # Driver code if __name__ == '__main__': N = 4; # Function Call printLargestSmallest(N); # This code is contributed by sapnasingh4991
C#
// C# program to find the largest // and smallest N-digit numbers // in Octal Number System using System; class GFG { // Function to return the largest // N-digit number in Octal // Number System static String findLargest(int N) { // Append '7' N times String largest = strings(N, '7'); return largest; } // Function to return the smallest // N-digit number in Octal // Number System static String findSmallest(int N) { // Append '0' (N - 1) times to 1 String smallest = "1" + strings((N - 1), '0'); return smallest; } private static String strings(int N, char c) { String temp =""; for(int i= 0; i < N; i++) { temp+=c; } return temp; } // Function to print the largest and // smallest N-digit Octal number static void printLargestSmallest(int N) { Console.Write("Largest: " + findLargest(N) +"\n"); Console.Write("Smallest: " + findSmallest(N) +"\n"); } // Driver code public static void Main(String[] args) { int N = 4; // Function Call printLargestSmallest(N); } } // This code is contributed by PrinciRaj1992
Javascript
<script> // Javascript program to find the largest // and smallest N-digit numbers // in Octal Number System // Function to return the largest // N-digit number in Octal // Number System function findLargest(N) { // Append '7' N times var largest = new Array(N+1).join( '7' ); return largest; } // Function to return the smallest // N-digit number in Octal // Number System function findSmallest(N) { // Append '0' (N - 1) times to 1 var smallest = "1" + new Array(N).join( '0' ); return smallest; } // Function to print the largest and // smallest N-digit Octal number function printLargestSmallest(N) { document.write("Largest: " + findLargest(N) + "<br>"); document.write( "Smallest: " + findSmallest(N)); } // Driver code var N = 4; // Function Call printLargestSmallest(N); </script>
Producción:
Largest: 7777 Smallest: 1000
Complejidad de tiempo: O(N) donde N es la longitud de la string.
Espacio Auxiliar: O(1)