📌  相关文章
📜  检查数字的二进制等效项是否以给定的字符串

📅  最后修改于: 2021-04-21 21:36:26             🧑  作者: Mango

给定正整数N ,任务是检查该整数的等效二进制数是否以给定的字符串str结尾。
如果以“ str”结尾,则打印“是”。否则,打印“否”。
例子

方法:想法是找到N的二进制等效项,并检查str是否为其二进制等效项的后缀。

下面是上述方法的实现:

C++
// C++ implementation of the
// above approach
 
#include 
using namespace std;
 
// Function returns true if
// s1 is suffix of s2
bool isSuffix(string s1, string s2)
{
    int n1 = s1.length();
    int n2 = s2.length();
    if (n1 > n2)
        return false;
    for (int i = 0; i < n1; i++)
        if (s1[n1 - i - 1] != s2[n2 - i - 1])
            return false;
    return true;
}
 
// Function to check if binary equivalent
// of a number ends in "111" or not
bool CheckBinaryEquivalent(int N, string str)
{
 
    // To store the binary
    // number
    int B_Number = 0;
    int cnt = 0;
 
    while (N != 0) {
 
        int rem = N % 2;
        int c = pow(10, cnt);
        B_Number += rem * c;
        N /= 2;
 
        // Count used to store
        // exponent value
        cnt++;
    }
 
    string bin = to_string(B_Number);
    return isSuffix(str, bin);
}
 
// Driver code
int main()
{
 
    int N = 23;
    string str = "111";
    if (CheckBinaryEquivalent(N, str))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}


Java
// Java implementation of the
// above approach
class GFG{
 
// Function returns true if
// s1 is suffix of s2
static boolean isSuffix(String s1, String s2)
{
    int n1 = s1.length(),
        n2 = s2.length();
         
    if (n1 > n2)
        return false;
         
    for(int i = 0; i < n1; i++)
        if (s1.charAt(n1 - i - 1) !=
            s2.charAt(n2 - i - 1))
        return false;
         
    return true;
}
 
// Function to check if binary equivalent
// of a number ends in "111" or not
static boolean CheckBinaryEquivalent(int N,
                                     String str)
{
 
    // To store the binary
    // number
    int B_Number = 0;
    int cnt = 0;
 
    while (N != 0)
    {
        int rem = N % 2;
        int c = (int)Math.pow(10, cnt);
        B_Number += rem * c;
        N /= 2;
 
        // Count used to store
        // exponent value
        cnt++;
    }
     
    String bin = Integer.toString(B_Number);
    return isSuffix(str, bin);
}
 
// Driver code
public static void main(String[] args)
{
    int N = 23;
    String str = "111";
     
    if (CheckBinaryEquivalent(N, str))
        System.out.print("Yes\n");
    else
        System.out.print("No\n");
}
}
 
// This code is contributed by shubham


Python3
# Python3 implementation of the
# above approach
  
# Function returns true if
# s1 is suffix of s2
def isSuffix(s1, s2):
 
    n1 = len(s1)
    n2 = len(s2)
     
    if(n1 > n2):
        return False
    for i in range(n1):
     
        if (s1[n1 - i - 1] !=
            s2[n2 - i - 1]):
            return False;
    return True;
 
# Function to check if
# binary equivalent of a
# number ends in "111" or not
def CheckBinaryEquivalent(N, s):
  
    # To store the binary
    # number
    B_Number = 0;
    cnt = 0;
  
    while (N != 0):
  
        rem = N % 2;
        c = pow(10, cnt);
        B_Number += rem * c;
        N //= 2;
  
        # Count used to store
        # exponent value
        cnt += 1;   
  
    bin = str(B_Number);   
    return isSuffix(s, bin);
   
# Driver code   
if __name__ == "__main__":
     
    N = 23;
    s = "111";
     
    if (CheckBinaryEquivalent(N, s)):
        print("Yes")
    else:
        print("No")
 
# This code is contributed by rutvik_56


C#
// C# implementation of the 
// above approach 
using System;
using System.Collections;
 
class GFG{
 
// Function returns true if
// s1 is suffix of s2
static bool isSuffix(String s1, String s2)
{
    int n1 = s1.Length,
        n2 = s2.Length;
         
    if (n1 > n2)
        return false;
         
    for(int i = 0; i < n1; i++)
        if (s1[n1 - i - 1] !=
            s2[n2 - i - 1])
        return false;
         
    return true;
}
 
// Function to check if binary equivalent
// of a number ends in "111" or not
static bool CheckBinaryEquivalent(int N,
                                  String str)
{
     
    // To store the binary
    // number
    int B_Number = 0;
    int cnt = 0;
 
    while (N != 0)
    {
        int rem = N % 2;
        int c = (int)Math.Pow(10, cnt);
        B_Number += rem * c;
        N /= 2;
 
        // Count used to store
        // exponent value
        cnt++;
    }
    String bin = B_Number.ToString();
    return isSuffix(str, bin);
}
 
// Driver Code
public static void Main (String[] args)
{
    int N = 23;
    String str = "111";
     
    if (CheckBinaryEquivalent(N, str))
        Console.WriteLine("Yes\n");
    else
        Console.WriteLine("No\n");
}
}
 
// This code is contributed by jana_sayantan


输出:
Yes