📜  如果在七段显示屏中显示,请检查数字的镜像是否相同

📅  最后修改于: 2021-04-27 22:01:07             🧑  作者: Mango

给定正数n 。任务是检查数字的镜像是否等于给定数字,如果显示在“七线段”上。数字的镜像是数字的反射重复,看起来几乎相同,但在垂直于镜面的方向上相反。

例子:

Input : n = 101
Output: Yes
Mirror image of 101 is 101 on seven line segment. So, print "Yes".

Input : n = 020
Output : No


当显示在七个线段上时,请观察每个数字,只有0、1、8的数字在它们的镜像中保持不变。因此,对于等于其镜像的数字,它应仅包含0、1、8位数字。另外,请注意,对于等于的两个数字,它们对应的位置数字应相同。因此,镜像在其相应的数字位置上也应包含相同的数字。因此,数量也应该是回文。

以下是此方法的实现:

C++
// C++ Program to check if mirror image of a number is
// same if displayed in seven segment display
#include 
using namespace std;
  
// Return "Yes", if the mirror image of number
// is same as the given number
// Else return "No"
string checkEqual(string S)
{
    // Checking if the number contain only 0, 1, 8.
    for (int i = 0; i < S.size(); i++) {
        if (S[i] != '1' && S[i] != '0' && S[i] != '8') {
            return "No";
        }
    }
  
    int start = 0, end = S.size() - 1;
  
    // Checking if the number is palindrome or not.
    while (start < end) {
  
        // If corresponding index is not equal.
        if (S[start] != S[end]) {
            return "No";
        }
  
        start++;
        end--;
    }
  
    return "Yes";
}
int main()
{
    string S = "101";
  
    cout << checkEqual(S) << endl;
    return 0;
}


Java
// Java Program to check if 
// mirror image of a number 
// is same if displayed in 
// seven segment display
import java.io.*;
  
class GFG
{
      
// Return "Yes", if the 
// mirror image of number
// is same as the given 
// number Else return "No"
static String checkEqual(String S)
{
    // Checking if the number
    // contain only 0, 1, 8.
    for (int i = 0; 
             i < S.length(); i++) 
    {
        if (S.charAt(i) != '1' && 
            S.charAt(i) != '0' && 
            S.charAt(i) != '8') 
        {
            return "No";
        }
    }
  
    int start = 0,
        end = S.length() - 1;
  
    // Checking if the number
    // is palindrome or not.
    while (start < end)
    {
  
        // If corresponding 
        // index is not equal.
        if (S.charAt(start) !=
            S.charAt(end))
        {
            return "No";
        }
  
        start++;
        end--;
    }
  
    return "Yes";
}
  
// Driver Code
public static void main (String[] args)
{
    String S = "101";
  
    System.out.println(checkEqual(S));
}
}
  
// This code is contributed
// by anuj_67.


Python3
# Python3 Program to check if mirror 
# image of a number is same if displayed 
# in seven segment display 
  
# Return "Yes", if the mirror image 
# of number is same as the given number 
# Else return "No" 
def checkEqual(S): 
      
    # Checking if the number contain 
    # only 0, 1, 8. 
    for i in range(len(S)): 
        if (S[i] != '1' and S[i] != '0' 
                        and S[i] != '8'): 
            return "No"; 
  
    start = 0;
    end = len(S) - 1; 
  
    # Checking if the number is 
    # palindrome or not. 
    while (start < end):
  
        # If corresponding index is not equal. 
        if (S[start] != S[end]):
            return "No"; 
  
        start += 1; 
        end -= 1;
  
    return "Yes"; 
  
# Driver Code
S = "101"; 
print(checkEqual(S)); 
      
# This code is contributed by mits


C#
// C# Program to check if mirror image 
// of a number is same if displayed in 
// seven segment display
using System;
  
class GFG
{
      
// Return "Yes", if the mirror image 
// of number is same as the given 
// number Else return "No"
static string checkEqual(string S)
{
    // Checking if the number
    // contain only 0, 1, 8.
    for (int i = 0; i < S.Length; i++) 
    {
        if (S[i] != '1' && 
            S[i] != '0' && 
            S[i] != '8') 
        {
            return "No";
        }
    }
  
    int start = 0, end = S.Length - 1;
  
    // Checking if the number is 
    // palindrome or not.
    while (start < end)
    {
  
        // If corresponding index is not equal.
        if (S[start] !=
            S[end])
        {
            return "No";
        }
  
        start++;
        end--;
    }
  
    return "Yes";
}
  
// Driver Code
public static void Main()
{
    string S = "101";
  
    Console.WriteLine(checkEqual(S));
}
}
  
// This code is contributed
// by mits


PHP


输出

Yes