给定整数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 corresponds to “1”, “10”, and “11”. Their concatenation results in “11011”, which corresponds to the decimal value of 27.
方法:想法是在[1,N]范围内进行迭代。对于每个第i个数字,使用按位XOR属性将数字i的二进制表示形式连接起来。请按照以下步骤解决问题:
- 用0初始化l和ans两个变量,其中l将位的当前位置存储在任何第i个数字的最终二进制字符串,而ans将存储最终答案。
- 从i = 1迭代到N +1 。
- 如果(i&(i – 1))等于0 ,则只需将l的值加1 ,其中&是按位AND运算符。
- 之后,将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
输出:
27
时间复杂度: O(N)
辅助空间: O(1)