📌  相关文章
📜  找到通过连接所有数字的二进制表示获得的数字,最多为 N

📅  最后修改于: 2021-11-18 02:48:26             🧑  作者: Mango

给定一个整数N ,任务是找到由顺序连接从1 到 N的所有数字的二进制表示形式形成的二进制字符串的十进制值。

例子:

方法:这个想法是迭代范围[1, N] 。对于每个i数字,使用 Bitwise XOR 属性连接数字i的二进制表示。请按照以下步骤解决问题:

  1. 0初始化两个变量lans ,其中l存储该位在任何i数字的最终二进制字符串中的当前位置, ans将存储最终答案。
  2. i = 1迭代到 N + 1
  3. 如果(i & ( i – 1 ))等于0 ,则只需将l的值增加1 ,其中&是按位与运算符。
  4. 之后,将ans左移l ,然后将结果与i进行按位或运算。
  5. 遍历后,打印ans作为答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the decimal value by
// concatenating the numbers from 1 to N
int concatenatedBinary(int n)
{
 
    // Stores count of
    // bits in a number
    int l = 0;
 
    // Stores decimal value by
    // concatenating 1 to N
    int ans = 0;
 
    // Iterate over the range [1, n]
    for (int i = 1; i < n + 1; i++){
 
        // If i is a power of 2
        if ((i & (i - 1)) == 0)
              l += 1;
 
        // Update ans
        ans = ((ans << l) | i);
    }
 
    // Return ans
    return ans;
}
 
// Driver Code
int main()
{
    int n = 3;
 
    // Function Call
    cout << (concatenatedBinary(n));
 
  return 0;
}
 
// This code is contributed by mohiy kumar 29


Java
// Java program for the above approach
class GFG
{
     
    // Function to find the decimal value by
    // concatenating the numbers from 1 to N
    static int concatenatedBinary(int n)
    {
     
        // Stores count of
        // bits in a number
        int l = 0;
     
        // Stores decimal value by
        // concatenating 1 to N
        int ans = 0;
     
        // Iterate over the range [1, n]
        for (int i = 1; i < n + 1; i++){
     
            // If i is a power of 2
            if ((i & (i - 1)) == 0)
                  l += 1;
     
            // Update ans
            ans = ((ans << l) | i);
        }
     
        // Return ans
        return ans;
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        int n = 3;
     
        // Function Call
        System.out.println(concatenatedBinary(n));
    }
}
 
// This code is contributed by AnkThon


Python3
# Python program for the above approach
 
# Function to find the decimal value by
# concatenating the numbers from 1 to N
def concatenatedBinary(n):
 
    # Stores count of
    # bits in a number
    l = 0
 
    # Stores decimal value by
    # concatenating 1 to N
    ans = 0
 
    # Iterate over the range [1, n]
    for i in range(1, n + 1):
 
        # If i is a power of 2
        if i & (i - 1) == 0:
 
            # Update l
            l += 1
 
        # Update ans
        ans = ((ans << l) | i)
 
    # Return ans
    return(ans)
 
# Driver Code
if __name__ == '__main__':
    n = 3
 
    # Function Call
    print(concatenatedBinary(n))


C#
// C# program to implement
// the above approach 
using System;
class GFG
{
     
    // Function to find the decimal value by
    // concatenating the numbers from 1 to N
    static int concatenatedBinary(int n)
    {
     
        // Stores count of
        // bits in a number
        int l = 0;
     
        // Stores decimal value by
        // concatenating 1 to N
        int ans = 0;
     
        // Iterate over the range [1, n]
        for (int i = 1; i < n + 1; i++)
        {
     
            // If i is a power of 2
            if ((i & (i - 1)) == 0)
                  l += 1;
     
            // Update ans
            ans = ((ans << l) | i);
        }
     
        // Return ans
        return ans;
    }
     
    // Driver Code
    public static void Main ()
    {
        int n = 3;
     
        // Function Call
        Console.WriteLine(concatenatedBinary(n));
    }
}
 
// This code is contributed by sanjoy_62


Javascript


输出:
27

时间复杂度: O(N)
辅助空间: O(1)