📌  相关文章
📜  检查数字的二进制表示形式在块中是否具有等于0和1的数字

📅  最后修改于: 2021-04-23 06:37:02             🧑  作者: Mango

给定整数N,任务是检查其等效二进制数是否具有等于0和1的连续块的频率。请注意,0和全为1的数字不被视为具有0和1的块数。

例子:

简单方法:首先将整数转换为其等效的二进制数。对于1或0的每个块,从左到右遍历二进制字符串,找到其长度并将其添加到集合中。如果设置的长度为1,则打印“是”,否则打印“否”。

C++
// C++ implementation of the above 
// approach 
#include 
using namespace std;
 
// Function to check
void hasEqualBlockFrequency(int N)
{
 
  // Converting integer to its equivalent 
  // binary number
  string S = bitset<3> (N).to_string();
  set p;
  int c = 1;
 
  for(int i = 0; i < S.length(); i++)
  {
 
    // If adjacent character are same 
    // then increase counter
    if (S[i] == S[i + 1])
      c += 1;
 
    else
    {
      p.insert(c);
      c = 1;
    }
    p.insert(c);
  }
 
  if (p.size() == 1)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
 
// Driver code
int main()
{
  int N = 5;
  hasEqualBlockFrequency(N);
  return 0;
}
 
// This code is contributed by divyesh072019


Java
// Java implementation of the above 
// approach 
import java.util.*;
  
class GFG{
    
// Function to check
static void hasEqualBlockFrequency(int N)
{
     
    // Converting integer to its equivalent 
    // binary number
    String S = Integer.toString(N,2);
    HashSet p = new HashSet();
    int c = 1;
      
    for(int i = 0; i < S.length() - 1; i++)
    {
         
        // If adjacent character are same 
        // then increase counter
        if (S.charAt(i) == S.charAt(i + 1))
            c += 1;
          
        else
        {
            p.add(c);
            c = 1;
        }
        p.add(c);
    }
      
    if (p.size() == 1)
        System.out.println("Yes");
    else
        System.out.println("No");
}
  
// Driver Code
public static void main(String []args)
{
    int N = 5;
      
    hasEqualBlockFrequency(N);
}
}
 
// This code is contributed by rutvik_56


Python3
# Python3 implementation of the above
# approach
   
# Function to check
def hasEqualBlockFrequency(N):
   
    # Converting integer to its equivalent
    # binary number
    S = bin(N).replace("0b", "")
    p = set()
    c = 1
 
    for i in range(len(S)-1):
 
        # If adjacent character are same
        # then increase counter
        if (S[i] == S[i + 1]):
            c += 1
 
        else:
            p.add(c)
            c = 1
 
        p.add(c)
 
    if (len(p) == 1):
        print("Yes")
    else:
        print("No")
 
# Driver code
N = 5
hasEqualBlockFrequency(N)


C#
// C# implementation of the above 
// approach 
using System;
using System.Collections.Generic;
 
class GFG{
   
// Function to check
static void hasEqualBlockFrequency(int N)
{
     
    // Converting integer to its equivalent 
    // binary number
    string S = Convert.ToString(N, 2);
    HashSet p = new HashSet();
    int c = 1;
     
    for(int i = 0; i < S.Length - 1; i++)
    {
         
        // If adjacent character are same 
        // then increase counter
        if (S[i] == S[i + 1])
            c += 1;
         
        else
        {
            p.Add(c);
            c = 1;
        }
        p.Add(c);
    }
     
    if (p.Count == 1)
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
 
// Driver Code
static void Main()
{
    int N = 5;
     
    hasEqualBlockFrequency(N);
}
}
 
// This code is contributed by divyeshrabadiya07


C++
// C++ program to check if a number has
// same counts of 0s and 1s in every
// block.
 
#include 
using namespace std;
 
// function to convert decimal to binary
bool isEqualBlock(int n)
{
    // Count same bits in last block
    int first_bit = n % 2;
    int first_count = 1;
    n = n / 2;
    while (n % 2 == first_bit && n > 0) {
        n = n / 2;
        first_count++;
    }
 
    // If n is 0 or it has all 1s,
    // then it is not considered to
    // have equal number of 0s and
    // 1s in blocks.
    if (n == 0)
        return false;
 
    // Count same bits in all remaining blocks.
    while (n > 0) {
 
        int first_bit = n % 2;
        int curr_count = 1;
        n = n / 2;
        while (n % 2 == first_bit) {
            n = n / 2;
            curr_count++;
        }
 
        if (curr_count != first_count)
            return false;
    }
 
    return true;
}
 
// Driver program to test above function
int main()
{
    int n = 51;
    if (isEqualBlock(n))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}


Java
// Java program to check if a number has
// same counts of 0s and 1s in every
// block.
import java.io.*;
 
class GFG
{
 
// function to convert decimal to binary
static boolean isEqualBlock(int n)
{
    // Count same bits in last block
    int first_bit = n % 2;
    int first_count = 1;
    n = n / 2;
    while (n % 2 == first_bit && n > 0)
    {
        n = n / 2;
        first_count++;
    }
 
    // If n is 0 or it has all 1s,
    // then it is not considered to
    // have equal number of 0s and
    // 1s in blocks.
    if (n == 0)
        return false;
 
    // Count same bits in all remaining blocks.
    while (n > 0)
    {
 
        first_bit = n % 2;
        int curr_count = 1;
        n = n / 2;
        while (n % 2 == first_bit)
        {
            n = n / 2;
            curr_count++;
        }
 
        if (curr_count != first_count)
            return false;
    }
    return true;
}
 
// Driver code
public static void main (String[] args)
{
    int n = 51;
    if (isEqualBlock(n))
        System.out.println( "Yes");
    else
        System.out.println("No");
}
}
 
// This code is contributed by inder_mca


Python3
# Python3 program to check if a number has
# same counts of 0s and 1s in every block.
 
# Function to convert decimal to binary
def isEqualBlock(n):
 
    # Count same bits in last block
    first_bit = n % 2
    first_count = 1
    n = n // 2
    while n % 2 == first_bit and n > 0:
        n = n // 2
        first_count += 1
 
    # If n is 0 or it has all 1s,
    # then it is not considered to
    # have equal number of 0s and
    # 1s in blocks.
    if n == 0:
        return False
 
    # Count same bits in all remaining blocks.
    while n > 0:
 
        first_bit = n % 2
        curr_count = 1
        n = n // 2
        while n % 2 == first_bit:
            n = n // 2
            curr_count += 1
         
        if curr_count != first_count:
            return False
     
    return True
 
# Driver Code
if __name__ == "__main__":
 
    n = 51
    if isEqualBlock(n):
        print("Yes")
    else:
        print("No")
     
# This code is contributed by
# Rituraj Jain


C#
// C# program to check if a number has
// same counts of 0s and 1s in every
// block.
 
using System;
 
class GFG
{
 
    // function to convert decimal to binary
    static bool isEqualBlock(int n)
    {
        // Count same bits in last block
        int first_bit = n % 2;
        int first_count = 1;
        n = n / 2;
        while (n % 2 == first_bit && n > 0)
        {
            n = n / 2;
            first_count++;
        }
     
        // If n is 0 or it has all 1s,
        // then it is not considered to
        // have equal number of 0s and
        // 1s in blocks.
        if (n == 0)
            return false;
     
        // Count same bits in all remaining blocks.
        while (n > 0)
        {
     
            first_bit = n % 2;
            int curr_count = 1;
            n = n / 2;
             
            while (n % 2 == first_bit)
            {
                n = n / 2;
                curr_count++;
            }
             
            if (curr_count != first_count)
                return false;
        }
        return true;
    }
     
    // Driver code
    public static void Main ()
    {
        int n = 51;
         
        if (isEqualBlock(n))
            Console.WriteLine( "Yes");
        else
            Console.WriteLine("No");
    }
}
 
// This code is contributed by Ryuga


PHP
 0)
    {
        $n = (int)($n / 2);
        $first_count++;
    }
 
    // If n is 0 or it has all 1s, then
    // it is not considered to have equal
    // number of 0s and 1s in blocks.
    if ($n == 0)
        return false;
 
    // Count same bits in all remaining blocks.
    while ($n > 0)
    {
        $first_bit = $n % 2;
        $curr_count = 1;
        $n = (int)($n / 2);
        while ($n % 2 == $first_bit)
        {
            $n = (int)($n / 2);
            $curr_count++;
        }
 
        if ($curr_count != $first_count)
            return false;
    }
 
    return true;
}
 
// Driver Code
$n = 51;
if (isEqualBlock($n))
    echo "Yes";
else
    echo "No";
 
// This code is contributed by
// Shivi_Aggarwal
?>


Javascript


输出:
Yes

优化的解决方案:我们从最后一点开始遍历。我们首先在最后一个块中计算相同位数。然后,我们遍历所有位,对于每个块,我们计算相同位的数量,如果此计数与第一个计数不同,则返回false。如果所有块的计数相同,则返回true。

C++

// C++ program to check if a number has
// same counts of 0s and 1s in every
// block.
 
#include 
using namespace std;
 
// function to convert decimal to binary
bool isEqualBlock(int n)
{
    // Count same bits in last block
    int first_bit = n % 2;
    int first_count = 1;
    n = n / 2;
    while (n % 2 == first_bit && n > 0) {
        n = n / 2;
        first_count++;
    }
 
    // If n is 0 or it has all 1s,
    // then it is not considered to
    // have equal number of 0s and
    // 1s in blocks.
    if (n == 0)
        return false;
 
    // Count same bits in all remaining blocks.
    while (n > 0) {
 
        int first_bit = n % 2;
        int curr_count = 1;
        n = n / 2;
        while (n % 2 == first_bit) {
            n = n / 2;
            curr_count++;
        }
 
        if (curr_count != first_count)
            return false;
    }
 
    return true;
}
 
// Driver program to test above function
int main()
{
    int n = 51;
    if (isEqualBlock(n))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}

Java

// Java program to check if a number has
// same counts of 0s and 1s in every
// block.
import java.io.*;
 
class GFG
{
 
// function to convert decimal to binary
static boolean isEqualBlock(int n)
{
    // Count same bits in last block
    int first_bit = n % 2;
    int first_count = 1;
    n = n / 2;
    while (n % 2 == first_bit && n > 0)
    {
        n = n / 2;
        first_count++;
    }
 
    // If n is 0 or it has all 1s,
    // then it is not considered to
    // have equal number of 0s and
    // 1s in blocks.
    if (n == 0)
        return false;
 
    // Count same bits in all remaining blocks.
    while (n > 0)
    {
 
        first_bit = n % 2;
        int curr_count = 1;
        n = n / 2;
        while (n % 2 == first_bit)
        {
            n = n / 2;
            curr_count++;
        }
 
        if (curr_count != first_count)
            return false;
    }
    return true;
}
 
// Driver code
public static void main (String[] args)
{
    int n = 51;
    if (isEqualBlock(n))
        System.out.println( "Yes");
    else
        System.out.println("No");
}
}
 
// This code is contributed by inder_mca

Python3

# Python3 program to check if a number has
# same counts of 0s and 1s in every block.
 
# Function to convert decimal to binary
def isEqualBlock(n):
 
    # Count same bits in last block
    first_bit = n % 2
    first_count = 1
    n = n // 2
    while n % 2 == first_bit and n > 0:
        n = n // 2
        first_count += 1
 
    # If n is 0 or it has all 1s,
    # then it is not considered to
    # have equal number of 0s and
    # 1s in blocks.
    if n == 0:
        return False
 
    # Count same bits in all remaining blocks.
    while n > 0:
 
        first_bit = n % 2
        curr_count = 1
        n = n // 2
        while n % 2 == first_bit:
            n = n // 2
            curr_count += 1
         
        if curr_count != first_count:
            return False
     
    return True
 
# Driver Code
if __name__ == "__main__":
 
    n = 51
    if isEqualBlock(n):
        print("Yes")
    else:
        print("No")
     
# This code is contributed by
# Rituraj Jain

C#

// C# program to check if a number has
// same counts of 0s and 1s in every
// block.
 
using System;
 
class GFG
{
 
    // function to convert decimal to binary
    static bool isEqualBlock(int n)
    {
        // Count same bits in last block
        int first_bit = n % 2;
        int first_count = 1;
        n = n / 2;
        while (n % 2 == first_bit && n > 0)
        {
            n = n / 2;
            first_count++;
        }
     
        // If n is 0 or it has all 1s,
        // then it is not considered to
        // have equal number of 0s and
        // 1s in blocks.
        if (n == 0)
            return false;
     
        // Count same bits in all remaining blocks.
        while (n > 0)
        {
     
            first_bit = n % 2;
            int curr_count = 1;
            n = n / 2;
             
            while (n % 2 == first_bit)
            {
                n = n / 2;
                curr_count++;
            }
             
            if (curr_count != first_count)
                return false;
        }
        return true;
    }
     
    // Driver code
    public static void Main ()
    {
        int n = 51;
         
        if (isEqualBlock(n))
            Console.WriteLine( "Yes");
        else
            Console.WriteLine("No");
    }
}
 
// This code is contributed by Ryuga

的PHP

 0)
    {
        $n = (int)($n / 2);
        $first_count++;
    }
 
    // If n is 0 or it has all 1s, then
    // it is not considered to have equal
    // number of 0s and 1s in blocks.
    if ($n == 0)
        return false;
 
    // Count same bits in all remaining blocks.
    while ($n > 0)
    {
        $first_bit = $n % 2;
        $curr_count = 1;
        $n = (int)($n / 2);
        while ($n % 2 == $first_bit)
        {
            $n = (int)($n / 2);
            $curr_count++;
        }
 
        if ($curr_count != $first_count)
            return false;
    }
 
    return true;
}
 
// Driver Code
$n = 51;
if (isEqualBlock($n))
    echo "Yes";
else
    echo "No";
 
// This code is contributed by
// Shivi_Aggarwal
?>

Java脚本


输出:
Yes