📜  二进制数(二进制中没有连续的1)

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

给定N,检查数字是否为Fibbinary Number。二进制数是整数,其二进制表示形式不包含连续的整数。

例子 :

Input : 10
Output : YES
Explanation: 1010 is the binary representation 
             of 10 which does not contains any 
             consecutive 1's.

Input : 11
Output : NO
Explanation: 1011 is the binary representation 
             of 11, which contains consecutive 
             1's

这样做的想法是将数字右移,直到n!= 0。对于每个1的二进制表示形式,检查找到的最后一位是否为1。通过执行(n&1)来获取整数的二进制表示形式的最后一位。如果二进制表示的最后一位为1,而右移之前的前一位也为1,则我们遇到连续的1。因此,我们得出的结论是,它不是一个基数。

前几个Fibonnary数中的一些是:

0, 2, 4, 8, 10, 16, 18, 20.......
CPP
// CPP program to check if a number 
// is fibinnary number or not
#include 
using namespace std;
  
// function to check if binary 
// representation of an integer 
// has consecutive 1s
bool checkFibinnary(int n) 
{
    // stores the previous last bit
    // initially as 0
    int prev_last = 0;
     
    while (n) 
    {
        // if current last bit and 
        // previous last bit is 1
        if ((n & 1) && prev_last)
            return false;
      
        // stores the last bit
        prev_last = n & 1;
      
        // right shift the number
        n >>= 1;
    }
  
    return true;
}
  
// Driver code to check above function
int main()
{
    int n = 10;
    if (checkFibinnary(n))
        cout << "YES";
    else
        cout << "NO";
    return 0;
}


Java
// Java program to check if a number 
// is fibinnary number or not
class GFG {
      
    // function to check if binary 
    // representation of an integer 
    // has consecutive 1s
    static boolean checkFibinnary(int n) 
    {
  
        // stores the previous last bit
        // initially as 0
        int prev_last = 0;
          
        while (n != 0) 
        {
              
            // if current last bit and 
            // previous last bit is 1
            if ((n & 1) != 0 && prev_last != 0)
              
                return false;
          
            // stores the last bit
            prev_last = n & 1;
          
            // right shift the number
            n >>= 1;
        }
      
        return true;
    }
      
    // Driver code to check above function
    public static void main(String[] args)
    {
        int n = 10;
  
        if (checkFibinnary(n) == true)
            System.out.println("YES");
        else
            System.out.println("NO");
    }
}
  
// This code is contributed by
// Smitha Dinesh Semwal


Python3
# Python 3 program to check if a
# number is fibinnary number or 
# not
  
# function to check if binary 
# representation of an integer 
# has consecutive 1s
def checkFibinnary(n): 
  
    # stores the previous last bit
    # initially as 0
    prev_last = 0
      
    while (n): 
      
        # if current last bit and 
        # previous last bit is 1
        if ((n & 1) and prev_last):
            return False
      
        # stores the last bit
        prev_last = n & 1
      
        # right shift the number
        n >>= 1
      
  
    return True
  
# Driver code
n = 10
  
if (checkFibinnary(n)):
    print("YES")
else:
    print("NO")
  
# This code is contributed by Smitha Dinesh Semwal


C#
// C# program to check if a number 
// is fibinnary number or not
using System;
  
class GFG {
      
    // function to check if binary 
    // representation of an integer 
    // has consecutive 1s
    static bool checkFibinnary(int n) 
    {
  
        // stores the previous last bit
        // initially as 0
        int prev_last = 0;
          
        while (n != 0) 
        {
              
            // if current last bit and 
            // previous last bit is 1
            if ((n & 1) != 0 && prev_last != 0)
              
                return false;
          
            // stores the last bit
            prev_last = n & 1;
          
            // right shift the number
            n >>= 1;
        }
      
        return true;
    }
      
    // Driver code to check above function
    public static void Main()
    {
        int n = 10;
  
        if (checkFibinnary(n) == true)
            Console.WriteLine("YES");
        else
            Console.WriteLine("NO");
    }
}
  
// This code is contributed by vt_m.


PHP
>= 1;
    }
    return true;
}
  
// Driver code
$n = 10;
if (checkFibinnary($n))
    echo "YES";
else
    echo "NO";
  
// This code is contributed by mits 
?>


输出 :

YES

时间复杂度: O(log(n))

二进制数(二进制中无连续的1)– O(1)方法