给定整数N ,任务是使用任意数量的7个分段显示来找到可以借助N个分段显示的最大数字。
例子:
Input: N = 4
Output: 11
Explanation:
Largest number that can be displayed with the help of 4 segments using 2 seven segment displays turned is 11.
Input: N = 7
Output: 711
Explanation:
Largest number that can be displayed by turning on seven segments is 711 with the help of 3 segments display set.
方法:
七段式显示中的主要观察结果是打开从0到9的任意数字以获取一定数量的段,如下所述:
如果仔细观察该问题,则数字N可以是偶数或奇数两种,应分别解决以下问题:
- 对于偶数:如上图所示,使用偶数段可以显示6个数字,即
0 - 6 1 - 2 2 - 5 4 - 4 6 - 6 9 - 6
如图所示,数字1使用最小的段数来显示数字。然后,甚至可以使用1来显示段数,每个位数中有2个段计数。
- 对于奇数:如上图所示,可以使用奇数个段显示5个数字,即
3 - 5 5 - 5 7 - 3 8 - 7
如图所示,数字7使用最少的奇数段数来显示数字。然后,可以使用7显示奇数个段,每个位数中有3个段计数。
算法:
- 如果给定的数字N为0或1 ,那么任何具有此位数的数字都无法显示。
- 如果给定的数字N为奇数,则最高有效位数为7,其余数字可以借助(N – 3)段显示,因为要显示7,它需要3个段。
- 如果给定数字N为偶数,则最高有效位将为1,而其余数字可借助(N – 2)段显示,因为要显示1,仅需要2个段。
- 数N递归地逐位处理。
举例说明:
给定数字N be – 11
Digit (from MSB to LSB) | N | Largest number from N using segment | Segments used | Segments remaining |
---|---|---|---|---|
1 | 11 | 7 | 3 | 8 |
2 | 8 | 1 | 2 | 6 |
3 | 6 | 1 | 2 | 4 |
4 | 4 | 1 | 2 | 2 |
5 | 2 | 1 | 2 | 0 |
然后,最大数量将是71111 。
下面是上述方法的实现:
C++
// C++ implementation to find the
// maximum number that can be
// using the N segments in
// N segments display
#include
using namespace std;
// Function to find the maximum
// number that can be displayed
// using the N segments
void segments(int n)
{
// Condition to check base case
if (n == 1 || n == 0) {
return;
}
// Condition to check if the
// number is even
if (n % 2 == 0) {
cout << "1";
segments(n - 2);
}
// Condition to check if the
// number is odd
else if (n % 2 == 1) {
cout << "7";
segments(n - 3);
}
}
// Driver Code
int main()
{
int n;
n = 11;
segments(n);
return 0;
}
Java
// Java implementation to find the
// maximum number that can be
// using the N segments in
// N segments display
class GFG {
// Function to find the maximum
// number that can be displayed
// using the N segments
static void segments(int n)
{
// Condition to check base case
if (n == 1 || n == 0) {
return;
}
// Condition to check if the
// number is even
if (n % 2 == 0) {
System.out.print("1");
segments(n - 2);
}
// Condition to check if the
// number is odd
else if (n % 2 == 1) {
System.out.print("7");
segments(n - 3);
}
}
// Driver Code
public static void main (String[] args)
{
int n;
n = 11;
segments(n);
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 implementation to find the
# maximum number that can be
# using the N segments in
# N segments display
# Function to find the maximum
# number that can be displayed
# using the N segments
def segments(n) :
# Condition to check base case
if (n == 1 or n == 0) :
return;
# Condition to check if the
# number is even
if (n % 2 == 0) :
print("1",end="");
segments(n - 2);
# Condition to check if the
# number is odd
elif (n % 2 == 1) :
print("7",end="");
segments(n - 3);
# Driver Code
if __name__ == "__main__" :
n = 11;
segments(n);
# This code is contributed by AnkitRai01
C#
// C# implementation to find the
// maximum number that can be
// using the N segments in
// N segments display
using System;
class GFG {
// Function to find the maximum
// number that can be displayed
// using the N segments
static void segments(int n)
{
// Condition to check base case
if (n == 1 || n == 0) {
return;
}
// Condition to check if the
// number is even
if (n % 2 == 0) {
Console.Write("1");
segments(n - 2);
}
// Condition to check if the
// number is odd
else if (n % 2 == 1) {
Console.Write("7");
segments(n - 3);
}
}
// Driver Code
public static void Main()
{
int n;
n = 11;
segments(n);
}
}
// This code is contributed by AnkitRai01
输出:
71111
性能分析:
- 时间复杂度:与上述方法一样,在最坏的情况下,存在递归调用占用O(N)时间的情况,因此时间复杂度将为O(N) 。
- 辅助空间复杂度:如上述方法,考虑到递归调用中使用的堆栈空间,则辅助空间复杂度将为O(N)