📜  三个数的总和与XOR

📅  最后修改于: 2021-05-25 05:33:18             🧑  作者: Mango

给定整数N。任务是计算整数AB的对数,以使A + B + N = A ^ B ^ N且A和B小于N。
例子:

方法
为了使三个数字的等于三个数字的xor与给定数字之一的异或,我们可以执行以下操作:

  1. 以二进制形式表示固定数字。
  2. 遍历固定数字的二进制扩展。
    • 如果找到1,则只有一个条件,即将其他两个数字的二进制位分别设为0和0。
    • 如果找到0,则将存在三个条件,即,您可以将二进制位设置为(0,0),(1,0)
      或(0,1)。
  3. 以下三位元组将永远不会进位,因此它们是有效的。
  4. 因此答案将是3 ^(二进制表示中的零数)

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
 
// Defining ull to unsigned long long int
typedef unsigned long long int ull;
 
// Function to calculate power of 3
ull calculate(int bit_cnt)
{
    ull res = 1;
    while (bit_cnt--) {
        res = res * 3;
    }
 
    return res;
}
 
// Function to return the count of the
// unset bit ( zeros )
int unset_bit_count(ull n)
{
    int count = 0;
    while (n) {
 
        // Check the bit is 0 or not
        if ((n & 1) == 0)
            count++;
        // Right shifting ( dividing by 2 )
        n = n >> 1;
    }
    return count;
}
 
// Driver Code
int main()
{
    ull n;
    n = 2;
 
    int count = unset_bit_count(n);
 
    ull ans = calculate(count);
 
    cout << ans << endl;
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
 
class GFG
{
 
// Function to calculate power of 3
static long calculate(int bit_cnt)
{
    long res = 1;
    while (bit_cnt-- > 0)
    {
        res = res * 3;
    }
 
    return res;
}
 
// Function to return the count of the
// unset bit ( zeros )
static int unset_bit_count(long n)
{
    int count = 0;
    while (n > 0)
    {
 
        // Check the bit is 0 or not
        if ((n & 1) == 0)
            count++;
             
        // Right shifting ( dividing by 2 )
        n = n >> 1;
    }
    return count;
}
 
// Driver Code
public static void main(String[] args)
{
    long n;
    n = 2;
 
    int count = unset_bit_count(n);
 
    long ans = calculate(count);
 
    System.out.println(ans);
}
}
 
// This code is contributed by PrinciRaj1992


Python3
# Python3 implementation of the approach
 
# Function to calculate power of 3
def calculate(bit_cnt):
 
    res = 1;
    while (bit_cnt > 0):
        bit_cnt -= 1;
        res = res * 3;
    return res;
 
# Function to return the count of the
# unset bit ( zeros )
def unset_bit_count(n):
 
    count = 0;
    while (n > 0):
         
        # Check the bit is 0 or not
        if ((n & 1) == 0):
            count += 1;
             
        # Right shifting ( dividing by 2 )
        n = n >> 1;
     
    return count;
 
# Driver Code
if __name__ == '__main__':
 
    n = 2;
 
    count = unset_bit_count(n);
 
    ans = calculate(count);
 
    print(ans);
 
# This code contributed by Rajput-Ji


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
// Function to calculate power of 3
static long calculate(int bit_cnt)
{
    long res = 1;
    while (bit_cnt-- > 0)
    {
        res = res * 3;
    }
 
    return res;
}
 
// Function to return the count of the
// unset bit (zeros)
static int unset_bit_count(long n)
{
    int count = 0;
    while (n > 0)
    {
 
        // Check the bit is 0 or not
        if ((n & 1) == 0)
            count++;
             
        // Right shifting ( dividing by 2 )
        n = n >> 1;
    }
    return count;
}
 
// Driver Code
public static void Main(String[] args)
{
    long n;
    n = 2;
 
    int count = unset_bit_count(n);
 
    long ans = calculate(count);
 
    Console.WriteLine(ans);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
3

时间复杂度:O(未设置位数)

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。