给定正整数N ,任务是找到最小的N位数字,以便按以下顺序对其执行以下操作将得到最大的N位数字:
- 将数字转换为其二进制编码的十进制形式。
- 将所有所得的半字节连接起来以形成一个二进制数。
- 从数字中删除最低有效N位。
- 将此获取的二进制数字转换为其十进制形式。
例子:
Input: N = 4
Output: 9990
Explanation:
Largest 4 digit number = 9999
BCD of 9999 = 1001 1001 1001 1001
Binary form = 1001100110011001
Replacing last 4 bits by 0000: 1001 1001 1001 0000 = 9990
Therefore, the minimum N-digit number that can generate 9999 is 9990
Input: N = 5
Output: 99980
Explanation:
Largest 5 digit number = 99999
BCD of 99999 = 1001 1001 1001 1001 1001
Binary for = 10011001100110011001
Replacing last 5 bits by 00000: 10011001100110000000 = 99980
Therefore, the minimum N-digit number that can generate 99999 is 99980
方法:可以根据以下对BCD编号的观察来解决该问题。请按照以下步骤解决问题:
- BCD中的每个半字节都不会增加到二进制形式的9的1001之外,因为最大的一位十进制数是9 。
- 因此,可以得出结论,通过将N个半字节组合在一起可以获得的最大二进制数是1001次串联N次,其十进制表示形式必须是9次串联N次的数字。
- 必须删除此二进制格式中的最后N个LSB。因此,这些位的值将不会有助于增大结果。因此,不必将最后N位保持为9,因为我们需要产生最大结果的最小数量。
- floor(N / 4)的值将为我们提供将被从中完全删除的半字节数。将这些半字节分配为0000,以最小化该数目。
- N / 4的其余部分为我们提供了执行上一步骤后从最后一个非零半字节的LSB转换为0的位数。
- 通过执行上述步骤形成的BCD,当转换为十进制时,会生成所需的最大N位数字。
下面是上述方法的实现:
C++
// C++ Program to implement
// the above approach
#include
using namespace std;
void maximizedNdigit(int n)
{
int count0s, count9s;
// If n is divisible by 4
if (n % 4 == 0) {
count0s = n / 4;
count9s = n - n / 4;
}
// Otherwise
else {
count0s = n / 4 + 1;
count9s = n - count0s;
count0s--;
}
while (count9s--)
cout << '9';
if (n % 4 != 0)
cout << '8';
while (count0s--)
cout << '0';
cout << endl;
}
// Driver Code
int main()
{
int n = 5;
maximizedNdigit(n);
}
Java
// Java program to implement
// the above approach
import java.io.*;
class GFG{
static void maximizedNdigit(int n)
{
int count0s, count9s;
// If n is divisible by 4
if (n % 4 == 0)
{
count0s = n / 4;
count9s = n - n / 4;
}
// Otherwise
else
{
count0s = n / 4 + 1;
count9s = n - count0s;
count0s--;
}
while (count9s != 0)
{
count9s--;
System.out.print('9');
}
if (n % 4 != 0)
System.out.print('8');
while (count0s != 0)
{
count0s--;
System.out.print('0');
}
System.out.println();
}
// Driver Code
public static void main(String[] args)
{
int n = 5;
maximizedNdigit(n);
}
}
// This code is contributed by sanjoy_62
Python3
# Python3 program to implement
# the above approach
def maximizedNdigit(n):
# If n is divisible by 4
if (n % 4 == 0):
count0s = n // 4
count9s = n - n // 4
# Otherwise
else:
count0s = n // 4 + 1
count9s = n - count0s
count0s -= 1
while (count9s):
print('9', end = "")
count9s -= 1
if (n % 4 != 0):
print('8', end = "")
while (count0s):
print('0', end = "")
count0s -= 1
print()
# Driver Code
if __name__ == "__main__":
n = 5
maximizedNdigit(n)
# This code is contributed by chitranayal
C#
// C# program to implement
// the above approach
using System;
class GFG{
static void maximizedNdigit(int n)
{
int count0s, count9s;
// If n is divisible by 4
if (n % 4 == 0)
{
count0s = n / 4;
count9s = n - n / 4;
}
// Otherwise
else
{
count0s = n / 4 + 1;
count9s = n - count0s;
count0s--;
}
while (count9s != 0)
{
count9s--;
Console.Write('9');
}
if (n % 4 != 0)
Console.Write('8');
while (count0s != 0)
{
count0s--;
Console.Write('0');
}
Console.WriteLine();
}
// Driver Code
public static void Main()
{
int n = 5;
maximizedNdigit(n);
}
}
// This code is contributed by sanjoy_62
Javascript
99980
时间复杂度: O(N)
辅助空间: O(1)
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。