📌  相关文章
📜  x <= n的值计数,其中(n XOR x)=(n – x)

📅  最后修改于: 2021-05-25 00:42:30             🧑  作者: Mango

给定整数n ,任务是找到满足n XOR x = n – x的可能值0≤x≤n的数量。
例子:

天真的方法:一种简单的方法是检查从0到n(包括两个端点)的所有值,并确定它们是否满足方程式。下面的代码实现了这种方法:
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include
using namespace std;
 
// Function to return the count of
// valid values of x
static int countX(int n)
{
    int count = 0;
 
    for (int i = 0; i <= n; i++)
    {
 
        // If n - x = n XOR x
        if (n - i == (n ^ i))
                count++;
    }
 
        // Return the required count;
        return count;
}
 
// Driver code
int main()
{
    int n = 5;
    int answer = countX(n);
    cout << answer;
}
 
// This code is contributed by
// Shivi_Aggarwal


Java
// Java implementation of the approach
public class GFG {
 
    // Function to return the count of
    // valid values of x
    static int countX(int n)
    {
        int count = 0;
 
        for (int i = 0; i <= n; i++) {
 
            // If n - x = n XOR x
            if (n - i == (n ^ i))
                count++;
        }
 
        // Return the required count;
        return count;
    }
 
    // Driver code
    public static void main(String args[])
    {
        int n = 5;
        int answer = countX(n);
        System.out.println(answer);
    }
}


Python3
# Python3 implementation of the approach
import math as mt
 
# Function to return the count of
# valid values of x
def countX(n):
    count = 0
 
    for i in range(n + 1):
 
        if n - i == (n ^ i):
            count += 1
 
    return count
 
# Driver Code
if __name__ == '__main__':
    n = 5
    answer = countX(n)
    print(answer)
 
# This code is contributed by
# Mohit kumar 29


C#
// C# implementation of the above approach
using System;
 
class GFG
{
 
    // Function to return the count of
    // valid values of x
    static int countX(int n)
    {
        int count = 0;
 
        for (int i = 0; i <= n; i++)
        {
 
            // If n - x = n XOR x
            if (n - i == (n ^ i))
                count++;
        }
 
        // Return the required count;
        return count;
    }
 
    // Driver code
    public static void Main()
    {
        int n = 5;
        int answer = countX(n);
        Console.WriteLine(answer);
    }
}
 
// This code is contributed by Ryuga


PHP


Javascript


C++
// C++ implementation of the approach
#include
using namespace std;
 
// Function to return the count of
// valid values of x
int countX(int n)
{
    // Convert n into binary String
    string binary = bitset<8>(n).to_string();
     
    // To store the count of 1s
    int count = 0;
    for (int i = 0; i < binary.length(); i++)
    {
        // If current bit is 1
        if (binary.at(i) == '1')
            count++;
    }
     
    // Calculating answer
    int answer = (int)pow(2, count);
    return answer;
}
 
// Driver code
int main()
{
    int n = 5;
    int answer = countX(n);
    cout << (answer);
}
 
// This code is contributed by Rajput-Ji


Java
// Java implementation of the approach
public class GFG {
 
    // Function to return the count of
    // valid values of x
    static int countX(int n)
    {
        // Convert n into binary String
        String binary = Integer.toBinaryString(n);
 
        // To store the count of 1s
        int count = 0;
 
        for (int i = 0; i < binary.length(); i++) {
 
            // If current bit is 1
            if (binary.charAt(i) == '1')
                count++;
        }
 
        // Calculating answer
        int answer = (int)Math.pow(2, count);
        return answer;
    }
 
    // Driver code
    public static void main(String args[])
    {
        int n = 5;
        int answer = countX(n);
        System.out.println(answer);
    }
}


Python 3
# Python3 implementation of the approach
 
# Function to return the count of
# valid values of x
def countX(n):
 
    # Convert n into binary String
    binary = "{0:b}".format(n)
 
    # To store the count of 1s
    count = 0
 
    for i in range(len(binary)):
 
        # If current bit is 1
        if (binary[i] == '1'):
            count += 1
 
    # Calculating answer
    answer = int(pow(2, count))
    return answer
 
# Driver code
if __name__ == "__main__":
     
    n = 5
    answer = countX(n)
    print(answer)
 
# This code is contributed by ita_c


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
// Function to return the count of
// valid values of x
static int countX(int n)
{
    // Convert n into binary String
    string binary = Convert.ToString(n, 2);
 
    // To store the count of 1s
    int count = 0;
 
    for (int i = 0; i < binary.Length; i++)
    {
 
        // If current bit is 1
        if (binary[i] == '1')
            count++;
    }
 
    // Calculating answer
    int answer = (int)Math.Pow(2, count);
    return answer;
}
 
// Driver code
public static void Main()
{
    int n = 5;
    int answer = countX(n);
    Console.WriteLine(answer);
}
}
 
// This code is contributed
// by Akanksha Rai


输出:
4

时间复杂度: O(N)
高效方法:n转换为其二进制表示形式。现在,对于二进制字符串中的每1 ,无论我们减去1还是0 ,都将等于1与0或1的XOR,即
(1-1)=(1 XOR 1)= 0
(1 – 0)=(1 XOR 0)= 1
但是0不满足此条件。因此,我们只需要考虑n的二进制表示形式中的所有那些。现在,每1有两种可能性,即0或1 。因此,如果我们在n中m个1的数目,那么我们的解将是2 m
下面是上述方法的实现:

C++

// C++ implementation of the approach
#include
using namespace std;
 
// Function to return the count of
// valid values of x
int countX(int n)
{
    // Convert n into binary String
    string binary = bitset<8>(n).to_string();
     
    // To store the count of 1s
    int count = 0;
    for (int i = 0; i < binary.length(); i++)
    {
        // If current bit is 1
        if (binary.at(i) == '1')
            count++;
    }
     
    // Calculating answer
    int answer = (int)pow(2, count);
    return answer;
}
 
// Driver code
int main()
{
    int n = 5;
    int answer = countX(n);
    cout << (answer);
}
 
// This code is contributed by Rajput-Ji

Java

// Java implementation of the approach
public class GFG {
 
    // Function to return the count of
    // valid values of x
    static int countX(int n)
    {
        // Convert n into binary String
        String binary = Integer.toBinaryString(n);
 
        // To store the count of 1s
        int count = 0;
 
        for (int i = 0; i < binary.length(); i++) {
 
            // If current bit is 1
            if (binary.charAt(i) == '1')
                count++;
        }
 
        // Calculating answer
        int answer = (int)Math.pow(2, count);
        return answer;
    }
 
    // Driver code
    public static void main(String args[])
    {
        int n = 5;
        int answer = countX(n);
        System.out.println(answer);
    }
}

的Python 3

# Python3 implementation of the approach
 
# Function to return the count of
# valid values of x
def countX(n):
 
    # Convert n into binary String
    binary = "{0:b}".format(n)
 
    # To store the count of 1s
    count = 0
 
    for i in range(len(binary)):
 
        # If current bit is 1
        if (binary[i] == '1'):
            count += 1
 
    # Calculating answer
    answer = int(pow(2, count))
    return answer
 
# Driver code
if __name__ == "__main__":
     
    n = 5
    answer = countX(n)
    print(answer)
 
# This code is contributed by ita_c

C#

// C# implementation of the approach
using System;
 
class GFG
{
 
// Function to return the count of
// valid values of x
static int countX(int n)
{
    // Convert n into binary String
    string binary = Convert.ToString(n, 2);
 
    // To store the count of 1s
    int count = 0;
 
    for (int i = 0; i < binary.Length; i++)
    {
 
        // If current bit is 1
        if (binary[i] == '1')
            count++;
    }
 
    // Calculating answer
    int answer = (int)Math.Pow(2, count);
    return answer;
}
 
// Driver code
public static void Main()
{
    int n = 5;
    int answer = countX(n);
    Console.WriteLine(answer);
}
}
 
// This code is contributed
// by Akanksha Rai
输出:
4

时间复杂度: $O(\log{}n)$