给定一个整数N ,任务是找到由顺序连接从1 到 N的所有数字的二进制表示形式形成的二进制字符串的十进制值。
例子:
Input: N = 12
Output: 118505380540
Explanation: The concatenation results in “1101110010111011110001001101010111100”. The equivalent decimal value is 118505380540.
Input: N = 3
Output: 27
Explanation: In binary, 1, 2, and 3 correspond to “1”, “10”, and “11”. Their concatenation results in “11011”, which corresponds to the decimal value of 27.
方法:这个想法是迭代范围[1, N] 。对于每个第i个数字,使用 Bitwise XOR 属性连接数字i的二进制表示。请按照以下步骤解决问题:
- 用0初始化两个变量l和ans ,其中l存储该位在任何第i个数字的最终二进制字符串中的当前位置, ans将存储最终答案。
- 从i = 1迭代到 N + 1 。
- 如果(i & ( i – 1 ))等于0 ,则只需将l的值增加1 ,其中&是按位与运算符。
- 之后,将ans左移l ,然后将结果与i进行按位或运算。
- 遍历后,打印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)