📜  在七段显示中使用最小段查找元素

📅  最后修改于: 2021-10-26 02:30:51             🧑  作者: Mango

七段显示器可用于显示数字。给定一个由n 个自然数组成的数组。任务是在数组中找到使用最小段来显示数字的数字。如果多个数字具有最小段数,则输出具有最小索引的数字。

七段显示

例子 :

Input : arr[] = { 1, 2, 3, 4, 5 }.
Output : 1
1 uses on 2 segments to display.

Input : arr[] = { 489, 206, 745, 123, 756 }.
Output : 745

预先计算 0 到 9 位数字使用的段数并存储。现在对数组的每个元素求和每个数字使用的段数。然后找到使用最少段数的元素。
数字使用的段数:
0 -> 6
1 -> 2
2 -> 5
3 -> 5
4 -> 4
5 -> 5
6 -> 6
7 -> 3
8 -> 7
9 -> 6
下面是这个方法的实现:

C++
// C++ program to find minimum number of segments
// required
#include
using namespace std;
 
// Precomputed values of segment used by digit 0 to 9.
const int seg[10] = { 6, 2, 5, 5, 4, 5, 6, 3, 7, 6};
 
// Return the number of segments used by x.
int computeSegment(int x)
{
    if (x == 0)
        return seg[0];
 
    int count = 0;
 
    // Finding sum of the segment used by
    // each digit of a number.
    while (x)
    {
        count += seg[x%10];
        x /= 10;
    }
 
    return count;
}
 
int elementMinSegment(int arr[], int n)
{
    // Initialising the minimum segment and minimum
    // number index.
    int minseg = computeSegment(arr[0]);
    int minindex = 0;
 
    // Finding and comparing segment used
    // by each number arr[i].
    for (int i = 1; i < n; i++)
    {
        int temp = computeSegment(arr[i]);
 
        // If arr[i] used less segment then update
        // minimum segment and minimum number.
        if (temp < minseg)
        {
            minseg   = temp;
            minindex = i;
        }
    }
 
    return arr[minindex];
}
 
// Driven Program
int main()
{
    int arr[] = {489, 206, 745, 123, 756};
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << elementMinSegment(arr, n) << endl;
    return 0;
}


Java
// Java program to find minimum
// number of segments required
import java.io.*;
 
class GFG {
     
// Precomputed values of segment
// used by digit 0 to 9.
static int []seg = { 6, 2, 5, 5, 4, 5, 6, 3, 7, 6};
 
// Return the number of segments used by x.
static int computeSegment(int x)
{
    if (x == 0)
        return seg[0];
 
    int count = 0;
 
    // Finding sum of the segment used by
    // each digit of a number.
    while (x > 0)
    {
        count += seg[x % 10];
        x /= 10;
    }
 
    return count;
}
 
static int elementMinSegment(int []arr, int n)
{
    // Initialising the minimum segment
    // and minimum number index.
    int minseg = computeSegment(arr[0]);
    int minindex = 0;
 
    // Finding and comparing segment used
    // by each number arr[i].
    for (int i = 1; i < n; i++)
    {
        int temp = computeSegment(arr[i]);
 
        // If arr[i] used less segment then update
        // minimum segment and minimum number.
        if (temp < minseg)
        {
            minseg = temp;
            minindex = i;
        }
    }
 
    return arr[minindex];
}
 
    // Driver program
    static public void main (String[] args)
    {
       int []arr = {489, 206, 745, 123, 756};
       int n = arr.length;
       System.out.println(elementMinSegment(arr, n));
    }
}
 
//This code is contributed by vt_m.


Python3
# Python implementation of
# the above approach
 
# Precomputed values of segment
# used by digit 0 to 9.
seg = [6, 2, 5, 5, 4,
       5, 6, 3, 7, 6]
 
# Return the number of
# segments used by x.
def computeSegment(x):
    if(x == 0):
        return seg[0]
 
    count = 0
 
    # Finding sum of the segment
    # used by each digit of a number.
    while(x):
        count += seg[x % 10]
        x = x // 10
 
    return count
 
# function to return minimum sum index
def elementMinSegment(arr, n):
 
    # Initialising the minimum
    # segment and minimum number index.
    minseg = computeSegment(arr[0])
    minindex = 0
 
    # Finding and comparing segment
    # used by each number arr[i].
    for i in range(1, n):
        temp = computeSegment(arr[i])
 
        # If arr[i] used less segment
        # then update minimum segment
        # and minimum number.
        if(temp < minseg):
 
            minseg = temp
            minindex = i
 
    return arr[minindex]
 
# Driver Code
arr = [489, 206, 745, 123, 756]
n = len(arr)
 
# function print required answer
print(elementMinSegment(arr, n))
 
# This code is contributed by
# Sanjit_Prasad


C#
// C# program to find minimum
// number of segments required
using System;
 
class GFG{
     
// Precomputed values of segment
// used by digit 0 to 9.
static int []seg = new int[10]{ 6, 2, 5, 5, 4,
                               5, 6, 3, 7, 6};
 
// Return the number of segments used by x.
static int computeSegment(int x)
{
    if (x == 0)
        return seg[0];
 
    int count = 0;
 
    // Finding sum of the segment used by
    // each digit of a number.
    while (x > 0)
    {
        count += seg[x % 10];
        x /= 10;
    }
 
    return count;
}
 
static int elementMinSegment(int []arr, int n)
{
    // Initialising the minimum segment
    // and minimum number index.
    int minseg = computeSegment(arr[0]);
    int minindex = 0;
 
    // Finding and comparing segment used
    // by each number arr[i].
    for (int i = 1; i < n; i++)
    {
        int temp = computeSegment(arr[i]);
 
        // If arr[i] used less segment then update
        // minimum segment and minimum number.
        if (temp < minseg)
        {
            minseg = temp;
            minindex = i;
        }
    }
 
    return arr[minindex];
}
 
    // Driver program
    static public void Main()
    {
       int []arr = {489, 206, 745, 123, 756};
       int n = arr.Length;
       Console.WriteLine(elementMinSegment(arr, n));
    }
}
 
//This code is contributed by vt_m.


PHP


Javascript


输出 :

745

时间复杂度: O(n * log 10 n)

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