📌  相关文章
📜  检查是否可以使用七段LED来显示数字

📅  最后修改于: 2021-04-29 14:32:53             🧑  作者: Mango

给定一个代表整数的字符串str和一个整数led ,它是可用LED的数量。任务是检查是否可以使用给定的LED显示号码。
请注意,在7段LED上显示时,将显示一个数字。如果可能显示数字,则打印“是” ,否则打印“否”

这是一个七段显示的示例:
七段LED

例子:

方法:预计算数字从09使用的段数并存储。现在,对于字符串的每个元素,计算其使用的段数。现在,如果count≤led,则打印YES,否则打印NO

下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
  
// Pre-computed values of segment used by digit 0 to 9.
const int seg[10] = { 6, 2, 5, 5, 4, 5, 6, 3, 7, 6 };
  
// Check if it is possible to display the number
string LedRequired(string s, int led)
{
    int count = 0;
  
    // Finding sum of the segments used by
    // each digit of the number
    for (int i = 0; i < s.length(); ++i) {
        count += seg[int(s[i]) - 48];
    }
  
    if (count <= led)
        return "YES";
    else
        return "NO";
}
  
// Driven Program
int main()
{
    string S = "123456789";
    int led = 20;
  
    // Function call to print required answer
    cout << LedRequired(S, led) << endl;
    return 0;
}


Java
// Java implementation of the above approach 
public class GfG{
  
    // Check if it is possible to display the number 
    public static String LedRequired(String s, int led) 
    { 
        // Pre-computed values of segment used by digit 0 to 9. 
        int seg[] = { 6, 2, 5, 5, 4, 5, 6, 3, 7, 6 }; 
  
        int count = 0; 
        // Finding sum of the segments used by 
        // each digit of the number 
        for (int i = 0; i < s.length(); ++i) { 
            count += seg[(int)(s.charAt(i)) - 48]; 
        } 
        
        if (count <= led) 
            return "YES"; 
        else
            return "NO"; 
    } 
  
    public static void main(String []args){
          
        String S = "123456789"; 
        int led = 20; 
        
        // Function call to print required answer 
        System.out.println(LedRequired(S, led));
    }
}
  
// This code is contributed by Rituraj Jain


Python3
# Python3 implementation of above approach 
  
# Pre-computed values of segment 
# used by digit 0 to 9. 
seg = [ 6, 2, 5, 5, 4, 
        5, 6, 3, 7, 6 ] 
  
# Check if it is possible to
# display the number 
def LedRequired(s, led) : 
  
    count = 0
  
    # Finding sum of the segments used 
    # by each digit of the number 
    for i in range(len(s)) :
        count += seg[ord(s[i]) - 48]
      
    if (count <= led) :
        return "YES"
    else :
        return "NO"
  
# Driver Code
if __name__ == "__main__" : 
  
    S = "123456789"
    led = 20
  
    # Function call to print
    # required answer 
    print(LedRequired(S, led)) 
  
# This code is contributed by Ryuga


C#
// C# implementation of the above approach 
using System;
class GFG
{
  
// Check if it is possible to display the number 
public static String LedRequired(string s, int led) 
{ 
    // Pre-computed values of segment 
    // used by digit 0 to 9. 
    int[] seg = { 6, 2, 5, 5, 4, 5, 6, 3, 7, 6 }; 
  
    int count = 0; 
      
    // Finding sum of the segments used by 
    // each digit of the number 
    for (int i = 0; i < s.Length; ++i)
    { 
        count += seg[(int)(s[i]) - 48]; 
    } 
  
    if (count <= led) 
        return "YES"; 
    else
        return "NO"; 
} 
  
// Driver Code
public static void Main()
{
      
    string S = "123456789"; 
    int led = 20; 
  
    // Function call to print required answer 
    Console.WriteLine(LedRequired(S, led));
}
}
  
// This code is contributed by Akanksha Rai


PHP


输出:
NO