📜  使用N个段可在七段显示器上显示的最大数量

📅  最后修改于: 2021-04-24 04:32:54             🧑  作者: Mango

给定正整数N。任务是使用N个段查找可以在七段显示屏上显示的最大数量。
七段式显示器:七段式显示器(SSD)或七段式指示器是一种电子显示设备,用于显示十进制数字,是更复杂的点矩阵显示的一种替代方式。

七段显示器的各个段
图片来源:维基百科。

例子:

Input : N = 5 
Output : 71
On 7-segment display, 71 will look like:
_
 | |
 | |

Input : N = 4
Output : 11

可以看到,位数比其他数字多的数字的价值会更大。因此,我们将尝试使用给定的“ N”个段来制作一个具有最大可能长度(数字位数)的数字。
还要注意,为了增加数字的长度,我们将尝试在每个数字上使用较少的段。因此,数字“ 1”仅使用2个段代表一个数字。没有其他数字使用少于2个段。
因此,在N为偶数的情况下,答案将是1s N / 2次。
在N为奇数的情况下,如果我们使1s N / 2次,则不能使用所有段。另外,如果我们使用3个段使数字7和1的(N-3)/ 2数,则形成的数的值将大于由N / 2的1数形成的数。
以下是此方法的实现:

C++
#include 
using namespace std;
 
// Function to print maximum number that can be formed
// using N segments
void printMaxNumber(int n)
{
    // If n is odd
    if (n & 1) {
        // use 3 three segment to print 7
        cout << "7";
 
        // remaining to print 1
        for (int i = 0; i < (n - 3) / 2; i++)
            cout << "1";
    }
 
    // If n is even
    else {
        // print n/2 1s.
        for (int i = 0; i < n / 2; i++)
            cout << "1";
    }
}
 
// Driver's Code
int main()
{
    int n = 5;
 
    printMaxNumber(n);
 
    return 0;
}


Java
// Java implementation of above approach
class GFG {
 
    // Function to print maximum number that
    // can be formed using N segments
    public static void printMaxNumber(int n)
    {
        // If n is odd
        if (n % 2 != 0) {
            // use 3 three segment to print 7
            System.out.print("7");
 
            // remaining to print 1
            for (int i = 0; i < (n - 3) / 2; i++)
                System.out.print("1");
        }
 
        // If n is even
        else {
 
            // print n/2 1s.
            for (int i = 0; i < n / 2; i++)
                System.out.print("1");
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int n = 5;
        printMaxNumber(n);
    }
}
 
// This code is contributed by princiraj1992


Python3
# Function to print maximum number that can be formed
# using N segments
def printMaxNumber(n):
     
    # If n is odd
    if (n % 2 == 1):
         
        # use 3 three segment to print 7
        print("7",end="");
 
        # remaining to print 1
        for i in range(int((n - 3) / 2)):
            print("1",end="");
 
    # If n is even
    else:
         
        # print n/2 1s.
        for i in range(n/2):
            print("1",end="");
 
# Driver's Code
n = 5;
printMaxNumber(n);
 
# This code contributed by Rajput-Ji


C#
// C# implementation of above approach
using System;
 
class GFG
{
 
    // Function to print maximum number that
    // can be formed using N segments
    public static void printMaxNumber(int n)
    {
        // If n is odd
        if (n % 2 != 0)
        {
            // use 3 three segment to print 7
            Console.Write("7");
 
            // remaining to print 1
            for (int i = 0; i < (n - 3) / 2; i++)
                Console.Write("1");
        }
 
        // If n is even
        else
        {
 
            // print n/2 1s.
            for (int i = 0; i < n / 2; i++)
                Console.Write("1");
        }
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        int n = 5;
        printMaxNumber(n);
    }
}
 
// This code has been contributed by 29AjayKumar


PHP


输出:
71

时间复杂度: O(n)

辅助空间: O(1)