📜  在右边的偶数位置找到一个包含N – 1个设置位的数字

📅  最后修改于: 2021-04-29 01:16:13             🧑  作者: Mango

给定正整数N ,任务是找到一个数字,该数字包含一个(N – 1)个二进制二进制形式的设置位,该数字位于右边的每个偶数索引(从1开始)处。

例子:

观察:如果我们以二进制形式检查数字,那么结果将是这样的:

n Decimal Equivalent Binary Equivalent
1 0 0
2 2 10
3 10 1010
4 42 101010
5 170 10101010

天真的方法:从表中可以看出,我们的等效二进制文件总是在前一个字符串的最后添加一个“ 10”。因此,我们可以生成一个由N-1次串联的子字符串“ 10”组成的二进制字符串,然后打印其等效的十进制数。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
#define ll long long int
  
// Function to return the string generated
// by appending "10" n-1 times
string constructString(ll n)
{
    // Initialising string as empty
    string s = "";
    for (ll i = 0; i < n; i++) {
        s += "10";
    }
    return s;
}
  
// Function to return the decimal equivalent
// of the given binary string
ll binaryToDecimal(string n)
{
    string num = n;
    ll dec_value = 0;
  
    // Initializing base value to 1
    // i.e 2^0
    ll base = 1;
  
    ll len = num.length();
    for (ll i = len - 1; i >= 0; i--) {
        if (num[i] == '1')
            dec_value += base;
        base = base * 2;
    }
  
    return dec_value;
}
  
// Function that calls the constructString
// and binarytodecimal and returns the answer
ll findNumber(ll n)
{
    string s = constructString(n - 1);
    ll num = binaryToDecimal(s);
    return num;
}
  
// Driver code
int main()
{
    ll n = 4;
  
    cout << findNumber(n);
  
    return 0;
}


Java
// Java implementation of above approach
import java.util.*;
  
class GFG 
{
  
// Function to return the String generated
// by appending "10" n-1 times
static String constructString(int n)
{
    // Initialising String as empty
    String s = "";
    for (int i = 0; i < n; i++) 
    {
        s += "10";
    }
    return s;
}
  
// Function to return the decimal equivalent
// of the given binary String
static int binaryToDecimal(String n)
{
    String num = n;
    int dec_value = 0;
  
    // Initializing base value to 1
    // i.e 2^0
    int base = 1;
  
    int len = num.length();
    for (int i = len - 1; i >= 0; i--) 
    {
        if (num.charAt(i) == '1')
            dec_value += base;
        base = base * 2;
    }
  
    return dec_value;
}
  
// Function that calls the constructString
// and binarytodecimal and returns the answer
static int findNumber(int n)
{
    String s = constructString(n - 1);
    int num = binaryToDecimal(s);
    return num;
}
  
// Driver code
public static void main(String[] args) 
{
    int n = 4;
  
    System.out.println(findNumber(n));
}
}
  
/* This code is contributed by PrinciRaj1992 */


Python
# Python3 implementation of the approach
  
# Function to return the generated
# by appending "10" n-1 times
def constructString(n):
  
    # Initialising as empty
    s = ""
    for i in range(n):
        s += "10"
  
    return s
  
# Function to return the decimal equivaLent
# of the given binary string
def binaryToDecimal(n):
  
    num = n
    dec_value = 0
  
    # Initializing base value to 1
    # i.e 2^0
    base = 1
  
    Len = len(num)
    for i in range(Len - 1,-1,-1):
        if (num[i] == '1'):
            dec_value += base
        base = base * 2
  
  
    return dec_value
  
# Function that calls the constructString
# and binarytodecimal and returns the answer
def findNumber(n):
  
    s = constructString(n - 1)
    num = binaryToDecimal(s)
    return num
  
# Driver code
n = 4
  
print(findNumber(n))
  
# This code is contributed by mohit kumar 29


C#
// C# implementation of above approach
using System;
  
class GFG
{
      
// Function to return the String generated
// by appending "10" n-1 times
static String constructString(int n)
{
      
    // Initialising String as empty
    String s = "";
    for (int i = 0; i < n; i++) 
    {
        s += "10";
    }
    return s;
}
  
// Function to return the decimal equivalent
// of the given binary String
static int binaryToDecimal(String n)
{
    String num = n;
    int dec_value = 0;
  
    // Initializing base value to 1
    // i.e 2^0
    int base_t = 1;
  
    int len = num.Length;
    for (int i = len - 1; i >= 0; i--) 
    {
        if (num[i] == '1')
            dec_value = dec_value + base_t;
        base_t = base_t * 2;
    }
  
    return dec_value;
}
  
// Function that calls the constructString
// and binarytodecimal and returns the answer
static int findNumber(int n)
{
    String s = constructString(n - 1);
    int num = binaryToDecimal(s);
    return num;
}
  
// Driver code
static public void Main ()
{
    int n = 4;
    Console.Write(findNumber(n));
}
}
  
// This code is contributed by ajit


C++
// C++ implementation of the approach
#include 
using namespace std;
  
#define ll long long int
  
// Function to compute number
// using our deduced formula
ll findNumber(int n)
{
    // Initialize num to n-1
    ll num = n - 1;
    num = 2 * (ll)pow(4, num);
    num = floor(num / 3.0);
    return num;
}
  
// Driver code
int main()
{
    int n = 5;
    cout << findNumber(n);
  
    return 0;
}


Java
// Java implementation of the approach
import java.io.*;
  
class GFG 
{
      
// Function to compute number
// using our deduced formula
static int findNumber(int n)
{
    // Initialize num to n-1
    int num = n - 1;
    num = 2 * (int)Math.pow(4, num);
    num = (int)Math.floor(num / 3.0);
    return num;
}
  
// Driver code
public static void main (String[] args) 
{
    int n = 5;
    System.out.println (findNumber(n));
}
}
  
// The code is contributed by ajit.


Python3
# Python3 implementation of the approach 
  
# Function to compute number 
# using our deduced formula 
def findNumber(n) :
      
    # Initialize num to n-1 
    num = n - 1;
    num = 2 * (4 ** num);
    num = num // 3;
    return num; 
  
# Driver code 
if __name__ == "__main__" :
      
    n = 5;
    print(findNumber(n)); 
      
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
  
class GFG
{
  
// Function to compute number
// using our deduced formula
static int findNumber(int n)
{
    // Initialize num to n-1
    int num = n - 1;
    num = 2 * (int)Math.Pow(4, num);
    num = (int)Math.Floor(num / 3.0);
    return num;
}
  
// Driver code
static public void Main ()
{
          
    int n = 5;
    Console.Write(findNumber(n));
}
}
  
// The code is contributed by Tushil.


输出:
42

高效的方法:如果我们将数字转换为4,我们可以看到一个有趣的模式,如下所示:

n Decimal Equivalent Binary Equivalent Base_4
1 0 0 0
2 2 10 2
3 10 1010 22
4 42 101010 222
5 170 10101010 2222

实际上,我们在base4中每n词末添加“ 2” ,即对于n = 7,我们在base4中的数字将为(n – 1),连续6个2
现在我们必须要牢记一点,因为我们知道如果我们将任何基数m转换为基数10(即小数),则解决方案是(n0 * m 0 + n1 * m 1 + n2 * m 2 +…。+ n * m n ) 。因此,通过进一步计算我们的底数为4 ,我们可以发现可以使用O(1)时间复杂度的推导公式来找到我们所需的数n
公式:

下面是上述方法的实现:

C++

// C++ implementation of the approach
#include 
using namespace std;
  
#define ll long long int
  
// Function to compute number
// using our deduced formula
ll findNumber(int n)
{
    // Initialize num to n-1
    ll num = n - 1;
    num = 2 * (ll)pow(4, num);
    num = floor(num / 3.0);
    return num;
}
  
// Driver code
int main()
{
    int n = 5;
    cout << findNumber(n);
  
    return 0;
}

Java

// Java implementation of the approach
import java.io.*;
  
class GFG 
{
      
// Function to compute number
// using our deduced formula
static int findNumber(int n)
{
    // Initialize num to n-1
    int num = n - 1;
    num = 2 * (int)Math.pow(4, num);
    num = (int)Math.floor(num / 3.0);
    return num;
}
  
// Driver code
public static void main (String[] args) 
{
    int n = 5;
    System.out.println (findNumber(n));
}
}
  
// The code is contributed by ajit.

Python3

# Python3 implementation of the approach 
  
# Function to compute number 
# using our deduced formula 
def findNumber(n) :
      
    # Initialize num to n-1 
    num = n - 1;
    num = 2 * (4 ** num);
    num = num // 3;
    return num; 
  
# Driver code 
if __name__ == "__main__" :
      
    n = 5;
    print(findNumber(n)); 
      
# This code is contributed by AnkitRai01

C#

// C# implementation of the approach
using System;
  
class GFG
{
  
// Function to compute number
// using our deduced formula
static int findNumber(int n)
{
    // Initialize num to n-1
    int num = n - 1;
    num = 2 * (int)Math.Pow(4, num);
    num = (int)Math.Floor(num / 3.0);
    return num;
}
  
// Driver code
static public void Main ()
{
          
    int n = 5;
    Console.Write(findNumber(n));
}
}
  
// The code is contributed by Tushil.
输出:
170