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 numérico hexadecimal .
Ejemplos:
Entrada: N = 1
Salida:
Par: E
Impar: F
Entrada: N = 2
Salida:
Par: FE
Impar: FF
Enfoque: para obtener el número más grande, los dígitos del número deben ser el máximo posible. Ya que en el sistema numérico hexadecimal, el dígito máximo es ‘F’ . Por lo tanto, genere ‘F’ (N – 1) veces y luego agregue ‘E’ para par y ‘F’ 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 hexadecimal number system void findNumbers(int n) { // Append 'F' (N - 1) times string ans = string(n - 1, 'F'); // Append 'E' for an even number string even = ans + 'E'; // Append 'F' for an odd number string odd = ans + 'F'; cout << "Even: " << even << endl; cout << "Odd: " << odd << endl; } // Driver code int main() { int n = 2; findNumbers(n); return 0; }
Java
// Java implementation of the approach class GFG { // Function to print the largest n-digit even // and odd numbers in hexadecimal number system static void findNumbers(int n) { // Append 'F' (N - 1) times String ans = string(n - 1, 'F'); // Append 'E' for an even number String even = ans + 'E'; // Append 'F' for an odd number String odd = ans + 'F'; System.out.print("Even: " + even + "\n"); System.out.print("Odd: " + odd + "\n"); } private static String string(int n, char c) { String str = ""; for (int i = 0; i < n; i++) str += c; return str; } // Driver code public static void main(String[] args) { int n = 2; 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 hexadecimal number system def findNumbers(n) : # Append 'F' (N - 1) times ans = 'F'*(n - 1); # Append 'E' for an even number even = ans + 'E'; # Append 'F' for an odd number odd = ans + 'F'; print("Even: " , even); print( "Odd: " , odd); # Driver code if __name__ == "__main__" : n = 2; 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 hexadecimal number system static void findNumbers(int n) { // Append 'F' (N - 1) times String ans = strings(n - 1, 'F'); // Append 'E' for an even number String even = ans + 'E'; // Append 'F' for an odd number String odd = ans + 'F'; Console.Write("Even: " + even + "\n"); Console.Write("Odd: " + odd + "\n"); } private static String strings(int n, char c) { String str = ""; for (int i = 0; i < n; i++) str += c; return str; } // Driver code public static void Main(String[] args) { int n = 2; 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 hexadecimal number system function findNumbers(n) { // Append 'F' (N - 1) times var ans = "F".repeat(n-1); // Append 'E' for an even number var even = ans + 'E'; // Append 'F' for an odd number var odd = ans + 'F'; document.write("Even: " + even + "<br>"); document.write("Odd: " + odd + "<br>"); } // Driver code var n = 2; findNumbers(n); </script>
Producción:
Even: FE Odd: FF
Complejidad de tiempo: O(1)
Espacio Auxiliar: O(1)