Seven-Segment Display

Alice got a number written in a seven-segment format where each segment was created used a matchstick.

Example: If Alice gets a number 123 so basically Alice used 12 matchsticks for this number.

Alice is wondering what is the numerically largest value that she can generate by using at most the matchsticks that she currently possesses. Help Alice out by telling her that number.

Example:

Input:  str = "0"
Output: 111

Approach

C++

#include <iostream>
using namespace std;

void sevenSegmentDisplay(string str)
{
    int arr[10] = {6255456376};
    int m = str.size();
    int a[m];

    for (int i = 0i < mi++)
    {
        a[i] = str[i] - '0';
    }

    int sum = 0;
    for (int i = 0i < mi++)
    {
        sum += arr[a[i]];
    }

    while (sum != 0)
    {
        if (sum % 2 != 0)
        {
            sum -= 3;
            cout << 7;
        }
        else
        {
            sum -= 2;
            cout << 1;
        }
    }
    cout << "\n";
}
int main()
{

    string str = "0";

    sevenSegmentDisplay(str);

    return 0;
}


No comments:

Post a Comment