📜  除数允许除以64

📅  最后修改于: 2021-04-23 05:53:07             🧑  作者: Mango

给定一个二进制字符串,我们需要在删除一些位后检查该数字是否可被64整除。如果是,则打印“可能”,否则打印“不可能”。我们不能将数字0整除。

例子 :

Input: 100010001 
Output: Possible
Explanation: We can get string 1 000 000 
after removing two ones which is a 
representation of number 64 in the binary 
numerical system.

Input: 100
Output: Not possible
Explanation : The number is 4 which is not 
divisible by 64 or cannot be made possible 
my removing some digits.

如果我们在任意一个零之后有6个零,那么我们可以删除将其表示为64的倍数的其他位。因此,我们只需要检查六个零之前是否存在1。

C++
// CPP program to find if given binary string can
// become divisible by 64 after removing some bits.
#include 
using namespace std;
  
// function to check if it is possible
// to make it a multiple of 64.
bool checking(string s)
{
    int c = 0; // counter to count 0's
    int n = s.length(); // length of the string
  
    // loop which traverses right to left and
    // calculates the number of zeros before 1.
    for (int i = n - 1; i >= 0; i--) {
        if (s[i] == '0')
            c++;
  
        if (c >= 6 and s[i] == '1')
            return true;
    }
  
    return false;
}
  
// driver code
int main()
{
    string s = "100010001";
    if (checking(s))
        cout << "Possible";
    else
        cout << "Not possible";
    return 0;
}


Java
// Java program to find if 
// given binary string can
// become divisible by 
// 64 after removing some bits
import java.io.*;
  
class GFG 
{
    // function to check if it is possible
    // to make it a multiple of 64.
    static boolean checking(String s)
    {
        // counter to count 0's
        int c = 0; 
          
        // length of the string
        int n = s.length(); 
      
        // loop which traverses right to left and
        // calculates the number of zeros before 1.
        for (int i = n - 1; i >= 0; i--) 
        {
            if (s.charAt(i) == '0')
                c++;
      
            if (c >= 6 && s.charAt(i) == '1')
                return true;
        }
      
        return false;
    }
  
    // Driver code
    public static void main (String[] args) 
    {
        String s = "100010001";
        if (checking(s))
            System.out.println ( "Possible");
        else
            System.out.println ( "Not possible");
      
    }
}
  
// This code  is contributed by vt_m


Python3
# Python 3 program to find if given binary 
# string can become divisible by 64 after
# removing some bits.
  
# function to check if it is possible
# to make it a multiple of 64.
def checking(s):
    c = 0
      
    # counter to count 0's
    n = len(s) 
      
    # length of the string
  
    # loop which traverses right to left and
    # calculates the number of zeros before 1.
    i = n - 1
    while(i >= 0):
        if (s[i] == '0'):
            c += 1
  
        if (c >= 6 and s[i] == '1'):
            return True
          
        i -= 1
      
    return False
  
# Driver code
if __name__ == '__main__':
    s = "100010001"
    if (checking(s)):
        print("Possible")
    else:
        print("Not possible")
      
# This code is contributed by
# Surendra_Gangwar


C#
// C# program to find if given binary
// string can become divisible by 64
// after removing some bits
using System;
  
class GFG {
      
    // function to check if it is possible
    // to make it a multiple of 64.
    static bool checking(string s)
    {
          
        // counter to count 0's
        int c = 0; 
          
        // length of the string
        int n = s.Length; 
      
        // loop which traverses right to
        // left and calculates the number
        // of zeros before 1.
        for (int i = n - 1; i >= 0; i--) 
        {
            if (s[i] == '0')
                c++;
      
            if (c >= 6 && s[i] == '1')
                return true;
        }
      
        return false;
    }
  
    // Driver code
    public static void Main () 
    {
        String s = "100010001";
          
        if (checking(s))
            Console.WriteLine (
                          "Possible");
        else
            Console.WriteLine(
                     "Not possible");
    }
}
  
// This code is contributed by vt_m.


PHP
= 0; $i--) 
    {
        if ($s[$i] == '0')
            $c++;
  
        if ($c >= 6 and $s[$i] == '1')
            return true;
    }
  
    return false;
}
  
// Driver Code
$s = "100010001";
if (checking($s))
    echo "Possible";
else
    echo "Not possible";
  
// This code is contributed by ajit
?>


输出 :

Possible

时间复杂度: O(字符串的长度)