Dado un número entero N , la tarea es encontrar los números de N dígitos pares e impares más grandes en el sistema de numeración octal.
Ejemplos:
Entrada: N = 4
Salida:
Par: 7776
Impar: 7777
Entrada: N = 2
Salida:
Par: 76
Impar: 77
Enfoque: para obtener el número más grande, los dígitos del número deben ser el máximo posible. Dado que en el sistema numérico octal, el dígito máximo es ‘7’ . Entonces, genere ‘7’ (N – 1) veces y luego agregue ‘6’ para par y ‘7’ para impar al final.
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 print the largest n-digit even // and odd numbers in octal number system void findNumbers(int n) { // Append '7' (N - 1) times string ans = string(n - 1, '7'); // Append '6' for an even number string even = ans + '6'; // Append '7' for an odd number string odd = ans + '7'; cout << "Even : " << even << endl; cout << "Odd : " << odd << endl; } // Driver code int main() { int n = 4; findNumbers(n); return 0; }
Java
// Java implementation of the approach class GFG { // Function to print the largest n-digit even // and odd numbers in octal number system static void findNumbers(int n) { // Append '7' (N - 1) times String ans = ""; for (int i = 0; i < n - 1; i++) ans += '7'; // Append '6' for an even number String even = ans + '6'; // Append '7' for an odd number String odd = ans + '7'; System.out.println("Even : " + even); System.out.println("Odd : " + odd); } // Driver code public static void main(String args[]) { int n = 4; findNumbers(n); } } // This code is contributed by 29AjayKumar
Python3
# Python3 implementation of the approach ; # Function to print the largest n-digit even # and odd numbers in octal number system def findNumbers(N) : # Append '7' (N - 1) times ans = '7' * (N - 1) # Append '6' for an even number even = ans + '6'; # Append '7' for an odd number odd = ans + '7'; print("Even : ", even); print("Odd : ", odd ); # Driver code if __name__ == "__main__" : n = 4; findNumbers(n); # This code is contributed by AnkitRai01
C#
// C# implementation of the approach using System; class GFG { // Function to print the largest n-digit even // and odd numbers in octal number system static void findNumbers(int n) { // Append '7' (N - 1) times String ans = ""; for (int i = 0; i < n - 1; i++) ans += '7'; // Append '6' for an even number String even = ans + '6'; // Append '7' for an odd number String odd = ans + '7'; Console.WriteLine("Even : " + even); Console.WriteLine("Odd : " + odd); } // Driver code public static void Main(String []args) { int n = 4; findNumbers(n); } } // This code is contributed by 29AjayKumar
Javascript
<script> // Javascript implementation of the approach // Function to print the largest n-digit even // and odd numbers in octal number system function findNumbers(n) { // Append '7' (N - 1) times var ans = ""; for (var i = 0; i < n - 1; i++) ans += '7'; // Append '6' for an even number var even = ans + '6'; // Append '7' for an odd number var odd = ans + '7'; document.write("Even : " + even + "<br>"); document.write("Odd : " + odd + "<br>"); } // Driver code var n = 4; findNumbers(n); // This code is contributed by Mayank Tyagi </script>
Producción:
Even : 7776 Odd : 7777
Complejidad de tiempo: O(n)
Espacio Auxiliar: O(1)