# 拆分二进制数以使每个部分都可被2整除的方式数量

2021年4月24日13:23:22 发表评论 872 次浏览

## C ++

``````//C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;

#define maxN 20
#define maxM 64

//Function to return the required count
int cntSplits(string s)
{
//If the splitting is not possible
if (s[s.size() - 1] == '1' )
return 0;

//To store the count of zeroes
int c_zero = 0;

//Counting the number of zeroes
for ( int i = 0; i <s.size(); i++)
c_zero += (s[i] == '0' );

return ( int ) pow (2, c_zero - 1);
}

//Driver code
int main()
{
string s = "10010" ;

cout <<cntSplits(s);

return 0;
}``````

## Java

``````//Java implementation of the approach
class GFG
{

static int maxN = 20 ;
static int maxM = 64 ;

//Function to return the required count
static int cntSplits(String s)
{
//If the splitting is not possible
if (s.charAt(s.length() - 1 ) == '1' )
return 0 ;

//To store the count of zeroes
int c_zero = 0 ;

//Counting the number of zeroes
for ( int i = 0 ; i <s.length(); i++)
c_zero += (s.charAt(i) == '0' ) ? 1 : 0 ;

return ( int )Math.pow( 2 , c_zero - 1 );
}

//Driver code
public static void main(String []args)
{
String s = "10010" ;

System.out.println(cntSplits(s));
}
}

//This code is contributed by PrinciRaj1992``````

## Python3

``````# Python3 implementation of the approach

# Function to return the required count
def cntSplits(s) :

# If the splitting is not possible
if (s[ len (s) - 1 ] = = '1' ) :
return 0 ;

# To store the count of zeroes
c_zero = 0 ;

# Counting the number of zeroes
for i in range ( len (s)) :
c_zero + = (s[i] = = '0' );

return int ( pow ( 2 , c_zero - 1 ));

# Driver code
if __name__ = = "__main__" :

s = "10010" ;

print (cntSplits(s));

# This code is contributed by AnkitRai01``````

## C#

``````//C# implementation of the approach
using System;

class GFG
{

static int maxN = 20;
static int maxM = 64;

//Function to return the required count
static int cntSplits(String s)
{
//If the splitting is not possible
if (s[s.Length - 1] == '1' )
return 0;

//To store the count of zeroes
int c_zero = 0;

//Counting the number of zeroes
for ( int i = 0; i <s.Length; i++)
c_zero += (s[i] == '0' ) ? 1 : 0;

return ( int )Math.Pow(2, c_zero - 1);
}

//Driver code
public static void Main(String []args)
{
String s = "10010" ;

Console.WriteLine(cntSplits(s));
}
}

//This code is contributed by 29AjayKumar``````

``4``