📌  相关文章
📜  查找通过级联所有数字(最多N个)的二进制表示而获得的数字

📅  最后修改于: 2021-04-27 22:52:43             🧑  作者: Mango

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

例子:

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

  1. 0初始化lans两个变量,其中l将位的当前位置存储在任何i数字的最终二进制字符串,而ans将存储最终答案。
  2. i = 1迭代到N +1
  3. 如果(i&(i – 1))等于0 ,则只需将l的值加1 ,其中是按位AND运算符。
  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


输出:
27

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