📜  不带前导零的N位二进制数的计数

📅  最后修改于: 2021-05-25 04:50:16             🧑  作者: Mango

给定一个整数N ,任务是查找不带前导零的N位二进制数的计数。
例子:

方法:由于数字不能有前导零,因此必须将最左边的位设置为1 。现在,对于其余的N – 1位,有两种选择,它们可以设置为01 。因此,可能的数量为2 N – 1
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the count
// of possible numbers
int count(int n)
{
    return pow(2, n - 1);
}
 
// Driver code
int main()
{
    int n = 4;
 
    cout << count(n);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
     
    // Function to return the count
    // of possible numbers
    static int count(int n)
    {
        return (int)Math.pow(2, n - 1);
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int n = 4;
     
        System.out.println(count(n));
    }
}
 
// This code is contributed by AnkitRai01


Python3
# Python3 implementation of the approach
 
# Function to return the count
# of possible numbers
def count(n):
    return pow(2, n - 1)
 
# Driver code
n = 4
 
print(count(n))
 
# This code is contributed by mohit kumar


C#
// C# implementation of the approach
using System;
     
class GFG
{
    // Function to return the count
    // of possible numbers
    static int count(int n)
    {
        return (int)Math.Pow(2, n - 1);
    }
     
    // Driver code
    public static void Main (String[] args)
    {
        int n = 4;
     
        Console.WriteLine(count(n));
    }
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
8