📜  使用 N 段的 7 段显示器上的最大数量:递归

📅  最后修改于: 2021-10-26 06:46:26             🧑  作者: Mango

给定一个整数N ,任务是找到可以在N段的帮助下使用任意数量的 7 段显示器显示的最大数字。
例子:

方法:
七段显示中的关键观察是打开从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 是01 ,那么任何数字都不能用这么多位显示。
  • 如果给定的数字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


Javascript


输出:
71111

性能分析:

  • 时间复杂度:与上述方法一样,递归调用在最坏的情况下需要 O(N) 时间,因此时间复杂度将为O(N)
  • 辅助空间复杂度:如上述方法,考虑到递归调用中使用的堆栈空间,则辅助空间复杂度为O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程