给定整数N ,任务是在十六进制十进制数系统中找到最小和最大的N位数字。
例子:
Input: N = 4
Output:
Largest: FFFF
Smallest: 1000
Input: N = 2
Output:
Largest: FF
Smallest: 10
方法:可以按照以下步骤计算所需的答案:
- 最大数字:要获得最大数字,数字的每个数字必须最大。十六进制十进制系统中的最大位数为’ F ‘。所以:
1 Digit Largest Number: 'F' 2 Digit Largest Number: 'FF' 3 Digit Largest Number: 'FFF' . . . N Digit Largest Number: 'FFF....(N) times'
- 最小数字:十六进制数字中的最小数字为’ 0 ‘。这个想法是,除0(为“ 1”)外,第一个数字应尽可能小,其余数字应为0 。所以:
1 Digit Smallest Number: '1' 2 Digit Smallest Number: '10' 3 Digit Smallest Number: '100' . . . N Digit Smallest Number: '100....(N - 1) times'
下面是上述方法的实现:
C++
// C++ program to find the largest
// and smallest N-digit numbers
// in Hexa-Decimal Number System
#include
using namespace std;
// Function to return the largest
// N-digit number in Hexa-Decimal
// Number System
string findLargest(int N)
{
// Append 'F' N times
string largest = string(N, 'F');
return largest;
}
// Function to return the smallest
// N-digit number in Hexa-Decimal
// 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 Hexa-Decimal number
void print(int largest)
{
cout << "Largest: " << findLargest(largest) << endl;
cout << "Smallest: " << findSmallest(largest) << endl;
}
// Driver code
int main()
{
int N = 4;
print(N);
return 0;
}
Java
// Java program to find the largest
// and smallest N-digit numbers
// in Hexa-Decimal Number System
class GFG {
// Function to return the largest
// N-digit number in Hexa-Decimal
// Number System
static String findLargest(int N)
{
String largest = "";
// Append 'F' N times
for (int i = 0; i < N ; i++)
largest += 'F';
return largest;
}
// Function to return the smallest
// N-digit number in Hexa-Decimal
// Number System
static String findSmallest(int N)
{
String smallest = "1" ;
// Append '0' (N - 1) times to 1
for(int i = 0; i < N - 1; i++)
smallest += '0';
return smallest;
}
// Function to print the largest and smallest
// N-digit Hexa-Decimal number
static void print(int largest)
{
System.out.println("Largest: " + findLargest(largest)) ;
System.out.println("Smallest: " + findSmallest(largest)) ;
}
// Driver code
public static void main (String[] args)
{
int N = 4;
print(N);
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 program to find the largest
# and smallest N-digit numbers
# in Hexa-Decimal Number System
# Function to return the largest
# N-digit number in Hexa-Decimal
# Number System
def findLargest(N) :
# Append 'F' N times
largest = 'F'*N
return largest;
# Function to return the smallest
# N-digit number in Hexa-Decimal
# Number System
def findSmallest(N) :
# Append '0' (N - 1) times to 1
smallest = '1' + '0'*(N - 1)
return smallest;
# Function to print the largest and smallest
# N-digit Hexa-Decimal number
def printAns(largest) :
print("Largest: " , findLargest(largest));
print("Smallest: " , findSmallest(largest));
# Driver code
if __name__ == "__main__" :
N = 4;
printAns(N);
# This code is contributed by AnkitRai01
C#
// C# program to find the largest
// and smallest N-digit numbers
// in Hexa-Decimal Number System
using System;
class GFG {
// Function to return the largest
// N-digit number in Hexa-Decimal
// Number System
static string findLargest(int N)
{
string largest = "";
// Append 'F' N times
for (int i = 0; i < N ; i++)
largest += 'F';
return largest;
}
// Function to return the smallest
// N-digit number in Hexa-Decimal
// Number System
static string findSmallest(int N)
{
string smallest = "1" ;
// Append '0' (N - 1) times to 1
for(int i = 0; i < N - 1; i++)
smallest += '0';
return smallest;
}
// Function to print the largest and smallest
// N-digit Hexa-Decimal number
static void print(int largest)
{
Console.WriteLine("Largest: " +
findLargest(largest)) ;
Console.WriteLine("Smallest: " +
findSmallest(largest)) ;
}
// Driver code
public static void Main(string[] args)
{
int N = 4;
print(N);
}
}
// This code is contributed by AnkitRai01
输出:
Largest: FFFF
Smallest: 1000
时间复杂度: O(N),其中N是字符串的长度。