📜  用2 ^ k检查二进制字符串的可除性

📅  最后修改于: 2021-05-25 10:32:10             🧑  作者: Mango

给定一个二进制字符串和一个数字k ,任务是检查二进制字符串是否可以被2 k整除。

例子 :

Input : 11000  k = 2
Output : Yes
Explanation :
(11000)2 = (24)10
24 is evenly divisible by 2k i.e. 4.
 
Input : 10101  k = 3
Output : No
Explanation : 
(10101)2 = (21)10
21 is not evenly divisible by 2k i.e. 8.

天真的方法:将二进制字符串计算为十进制,然后简单地检查它是否可以被2 k整除。但是,这种方法对于长二进制字符串不可行,因为从二进制字符串计算十进制数然后将其除以2 k的时间复杂度将很高。

高效方法:在二进制字符串,检查最后k位。如果最后k个位全部为0,则二进制数可以被2 k整除,否则就不能被整除。使用此方法的时间复杂度为O(k)。

下面是该方法的实现。

C++
// C++ implementation to check whether
// given binary number is evenly
// divisible by 2^k or not
#include 
using namespace std;
 
// function to check whether
// given binary number is
// evenly divisible by 2^k or not
bool isDivisible(char str[], int k)
{
    int n = strlen(str);
    int c = 0;
     
    // count of number of 0 from last
    for (int i = 0; i < k; i++)   
        if (str[n - i - 1] == '0')        
            c++;
     
    // if count = k, number is evenly
    // divisible, so returns true else
    // false
    return (c == k);
}
 
// Driver program to test above
int main()
{
    // first example
    char str1[] = "10101100";
    int k = 2;
    if (isDivisible(str1, k))
        cout << "Yes" << endl;
    else
        cout << "No"
             << "\n";
 
    // Second example
    char str2[] = "111010100";
    k = 2;
    if (isDivisible(str2, k))
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
 
    return 0;
}


Java
// Java implementation to check whether
// given binary number is evenly
// divisible by 2^k or not
class GFG {
     
    // function to check whether
    // given binary number is
    // evenly divisible by 2^k or not
    static boolean isDivisible(String str, int k)
    {
        int n = str.length();
        int c = 0;
     
        // count of number of 0 from last
        for (int i = 0; i < k; i++)
            if (str.charAt(n - i - 1) == '0')        
                c++;
     
        // if count = k, number is evenly
        // divisible, so returns true else
        // false
        return (c == k);
    }
 
    // Driver program to test above
    public static void main(String args[])
    {
         
        // first example
        String str1 = "10101100";
        int k = 2;
        if (isDivisible(str1, k) == true)
            System.out.println("Yes");
        else
            System.out.println("No");
 
    // Second example
        String str2 = "111010100";
        k = 2;
        if (isDivisible(str2, k) == true)
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
 
// This code is contributed by JaideepPyne.


Python3
# Python 3 implementation to check
# whether given binary number is
# evenly divisible by 2^k or not
 
# function to check whether
# given binary number is
# evenly divisible by 2^k or not
def isDivisible(str, k):
    n = len(str)
    c = 0
     
    # count of number of 0 from last
    for i in range(0, k):
        if (str[n - i - 1] == '0'):    
            c += 1
     
    # if count = k, number is evenly
    # divisible, so returns true else
    # false
    return (c == k)
 
# Driver program to test above
# first example
str1 = "10101100"
k = 2
if (isDivisible(str1, k)):
    print("Yes")
else:
    print("No")
 
# Second example
str2 = "111010100"
k = 2
if (isDivisible(str2, k)):
    print("Yes")
else:
    print("No")
 
# This code is contributed by Smitha


C#
// C# implementation to check whether
// given binary number is evenly
// divisible by 2^k or not
using System;
 
class GFG {
     
    // function to check whether
    // given binary number is
    // evenly divisible by 2^k or not
    static bool isDivisible(String str, int k)
    {
        int n = str.Length;
        int c = 0;
     
        // count of number of 0 from last
        for (int i = 0; i < k; i++)
            if (str[n - i - 1] == '0')    
                c++;
     
        // if count = k, number is evenly
        // divisible, so returns true else
        // false
        return (c == k);
    }
 
    // Driver program to test above
    public static void Main()
    {
         
        // first example
        String str1 = "10101100";
        int k = 2;
         
        if (isDivisible(str1, k) == true)
            Console.Write("Yes\n");
        else
            Console.Write("No");
 
        // Second example
        String str2 = "111010100";
        k = 2;
         
        if (isDivisible(str2, k) == true)
            Console.Write("Yes");
        else
            Console.Write("No");
    }
}
 
// This code is contributed by Smitha.


PHP


Javascript


输出:
Yes
Yes

时间复杂度:O(k)