📌  相关文章
📜  检查给定二进制字符串的十进制表示是否可被 K 整除

📅  最后修改于: 2022-05-13 01:56:08.098000             🧑  作者: Mango

检查给定二进制字符串的十进制表示是否可被 K 整除

给定一个二进制字符串S ,任务是找出给定二进制字符串的十进制表示是否可以被整数K整除。

例子:

方法:由于模运算符对加法是可分配的,因此可以逐位检查给定的二进制字符串以查看十进制 % k是否等于0 。请按照以下步骤操作:

  • 初始化一个二进制字符串大小的数组poweroftwo[] ,以存储 2 的幂。
  • 迭代直到size并且对于每个ipoweroftwo[]中存储2 i % K。
  • 初始化变量,比如rem = 0,将当前剩余数字存储到i
  • 迭代直到size并且对于每个i ,如果S[size – i -1]1 ,则更新rem等于rem + poweroftwo[i]。
  • 最后,如果 rem 等于0 ,则返回Yes ,否则返回No。

下面是上述方法的实现:

C++
// C++ program for above approach
#include 
using namespace std;
 
// Function to check the binary number is
// divisible by K
string divisibleByk(string s, int n, int k)
{
    // Array poweroftwo will store pow(2, i)%k
    int poweroftwo[n];
 
    // Initializing the first element in Array
    poweroftwo[0] = 1 % k;
 
    for (int i = 1; i < n; i++) {
 
        // Storing every pow(2, i)%k value in
        // the array
        poweroftwo[i] = (poweroftwo[i - 1]
                         * (2 % k))
                        % k;
    }
 
    // To store the remaining
    int rem = 0;
 
    // Iterating till N
    for (int i = 0; i < n; i++) {
 
        // If current bit is 1
        if (s[n - i - 1] == '1') {
 
            // Updating rem
            rem += (poweroftwo[i]);
            rem %= k;
        }
    }
 
    // If completely divisible
    if (rem == 0) {
        return "Yes";
    }
 
    // If not Completely divisible
    else
        return "No";
}
 
// Driver Code
int main()
{
    // Given Input
    string s = "1010001";
    int k = 9;
 
    // length of string s
    int n = s.length();
 
    // Function Call
    cout << divisibleByk(s, n, k);
    return 0;
}


Java
// Java program for above approach
class GFG
{
   
    // Function to check the binary number is
    // divisible by K
    public static String divisibleByk(String s, int n, int k) {
        // Array poweroftwo will store pow(2, i)%k
        int[] poweroftwo = new int[n];
 
        // Initializing the first element in Array
        poweroftwo[0] = 1 % k;
 
        for (int i = 1; i < n; i++) {
 
            // Storing every pow(2, i)%k value in
            // the array
            poweroftwo[i] = (poweroftwo[i - 1] * (2 % k)) % k;
        }
 
        // To store the remaining
        int rem = 0;
 
        // Iterating till N
        for (int i = 0; i < n; i++) {
 
            // If current bit is 1
            if (s.charAt(n - i - 1) == '1') {
 
                // Updating rem
                rem += (poweroftwo[i]);
                rem %= k;
            }
        }
 
        // If completely divisible
        if (rem == 0) {
            return "Yes";
        }
 
        // If not Completely divisible
        else
            return "No";
    }
 
    // Driver Code
    public static void main(String args[])
    {
 
      // Given Input
        String s = "1010001";
        int k = 9;
 
        // length of string s
        int n = s.length();
 
        // Function Call
        System.out.println(divisibleByk(s, n, k));
    }
 
}
 
// This code is contributed by _saurabh_jaiswal,


Python3
# python 3 program for above approach
 
# Function to check the binary number is
# divisible by K
def divisibleByk(s, n, k):
   
    # Array poweroftwo will store pow(2, i)%k
    poweroftwo = [0 for i in range(n)]
 
    # Initializing the first element in Array
    poweroftwo[0] = 1 % k
 
    for i in range(1,n,1):
        # Storing every pow(2, i)%k value in
        # the array
        poweroftwo[i] = (poweroftwo[i - 1] * (2 % k)) % k
 
    # To store the remaining
    rem = 0
 
    # Iterating till N
    for i in range(n):
       
        # If current bit is 1
        if (s[n - i - 1] == '1'):
 
            # Updating rem
            rem += (poweroftwo[i])
            rem %= k
 
    # If completely divisible
    if (rem == 0):
        return "Yes"
 
    # If not Completely divisible
    else:
        return "No"
 
# Driver Code
if __name__ == '__main__':
   
    # Given Input
    s = "1010001"
    k = 9
 
    # length of string s
    n = len(s)
 
    # Function Call
    print(divisibleByk(s, n, k))
     
    # This code is contributed by SURENDRA_GANGWAR.


C#
// C# program for above approach
using System;
class GFG
{
   
    // Function to check the binary number is
    // divisible by K
    public static String divisibleByk(String s, int n, int k) {
        // Array poweroftwo will store pow(2, i)%k
        int[] poweroftwo = new int[n];
 
        // Initializing the first element in Array
        poweroftwo[0] = 1 % k;
 
        for (int i = 1; i < n; i++) {
 
            // Storing every pow(2, i)%k value in
            // the array
            poweroftwo[i] = (poweroftwo[i - 1] * (2 % k)) % k;
        }
 
        // To store the remaining
        int rem = 0;
 
        // Iterating till N
        for (int i = 0; i < n; i++) {
 
            // If current bit is 1
            if (s[n - i - 1] == '1') {
 
                // Updating rem
                rem += (poweroftwo[i]);
                rem %= k;
            }
        }
 
        // If completely divisible
        if (rem == 0) {
            return "Yes";
        }
 
        // If not Completely divisible
        else
            return "No";
    }
 
    // Driver Code
    public static void Main(String []args)
    {
 
      // Given Input
        String s = "1010001";
        int k = 9;
 
        // length of string s
        int n = s.Length;
 
        // Function Call
        Console.Write(divisibleByk(s, n, k));
    }
 
}
 
// This code is contributed by shivanisinghss2110


Javascript


输出
Yes

时间复杂度: O(N)
辅助空间: O(N)