给定整数N ,任务是在十六进制数系统中找到最大的偶数和奇数N位数字。
例子:
Input: N = 1
Output:
Even: E
Odd: F
Input: N = 2
Output:
Even: FE
Odd: FF
方法:要获得最大的数字,该数字必须尽可能多。由于在十六进制系统中,最大位数为‘F’ 。因此,生成‘F’ (N – 1)次,然后在末尾附加‘E’表示偶数和‘F’表示奇数。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
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
输出:
Even: FE
Odd: FF