📌  相关文章
📜  从给定的按位异或和按位与所有对的值中找到三元组

📅  最后修改于: 2021-10-25 06:26:11             🧑  作者: Mango

给定六个正整数,表示三元组(a, b, c)的所有可能对的按位异或和按位与,任务是找到三元组。

例子:

方法:这个想法是根据以下观察,使用它们的按位异或和按位与值找到每对可能的三元组的总和:

请按照以下步骤解决问题:

  • 使用上述公式找到每对可能的三元组的总和,即(a + b, b + c, c + a)
  • a的值可以被计算为((A + B)+(A + C) – (B + C))/ 2。
  • b的值可以计算为((a + b) – a)
  • c的值可以计算为((b + c) – b)
  • 最后,打印三元组(a, b, c) 的值

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to find the triplet with given
// Bitwise XOR and Bitwise AND values of all
// possible pairs of the triplet
void findNumbers(int aXORb, int aANDb, int aXORc, int aANDc,
                 int bXORc, int bANDc)
{
    // Stores values of
    // a triplet
    int a, b, c;
 
    // Stores a + b
    int aSUMb;
 
    // Stores a + c
    int aSUMc;
 
    // Stores b + c
    int bSUMc;
 
    // Calculate aSUMb
    aSUMb = aXORb + aANDb * 2;
 
    // Calculate aSUMc
    aSUMc = aXORc + aANDc * 2;
 
    // Calculate bSUMc
    bSUMc = bXORc + bANDc * 2;
 
    // Calculate a
    a = (aSUMb - bSUMc + aSUMc) / 2;
 
    // Calculate b
    b = aSUMb - a;
 
    // Calculate c
    c = aSUMc - a;
 
    // Print a
    cout << "a = " << a;
 
    // Print b
    cout << ", b = " << b;
 
    // Print c
    cout << ", c = " << c;
}
 
// Driver Code
int main()
{
    int aXORb = 30, aANDb = 0, aXORc = 20, aANDc = 10,
        bXORc = 10, bANDc = 20;
 
    findNumbers(aXORb, aANDb, aXORc, aANDc, bXORc, bANDc);
}


Java
// Java program to implement
// the above approach
class GFG{
 
// Function to find the triplet with given
// Bitwise XOR and Bitwise AND values of all
// possible pairs of the triplet
static void findNumbers(int aXORb, int aANDb,
                        int aXORc, int aANDc,
                        int bXORc, int bANDc)
{
     
    // Stores values of
    // a triplet
    int a, b, c;
 
    // Stores a + b
    int aSUMb;
 
    // Stores a + c
    int aSUMc;
 
    // Stores b + c
    int bSUMc;
 
    // Calculate aSUMb
    aSUMb = aXORb + aANDb * 2;
 
    // Calculate aSUMc
    aSUMc = aXORc + aANDc * 2;
 
    // Calculate bSUMc
    bSUMc = bXORc + bANDc * 2;
 
    // Calculate a
    a = (aSUMb - bSUMc + aSUMc) / 2;
 
    // Calculate b
    b = aSUMb - a;
 
    // Calculate c
    c = aSUMc - a;
 
    // Print a
    System.out.print("a = " + a);
 
    // Print b
    System.out.print(", b = " + b);
 
    // Print c
    System.out.print(", c = " + c);
}
 
// Driver Code
public static void main(String[] args)
{
    int aXORb = 30, aANDb = 0,
        aXORc = 20, aANDc = 10,
        bXORc = 10, bANDc = 20;
 
    findNumbers(aXORb, aANDb, aXORc,
                aANDc, bXORc, bANDc);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python program to implement
# the above approach
 
# Function to find the triplet with given
# Bitwise XOR and Bitwise AND values of all
# possible pairs of the triplet
def findNumbers(aXORb, aANDb, aXORc, aANDc, bXORc, bANDc):
 
    # Stores values of
    # a triplet
    a, b, c = 0, 0, 0;
 
    # Stores a + b
    aSUMb = 0;
 
    # Stores a + c
    aSUMc = 0;
 
    # Stores b + c
    bSUMc = 0;
 
    # Calculate aSUMb
    aSUMb = aXORb + aANDb * 2;
 
    # Calculate aSUMc
    aSUMc = aXORc + aANDc * 2;
 
    # Calculate bSUMc
    bSUMc = bXORc + bANDc * 2;
 
    # Calculate a
    a = (aSUMb - bSUMc + aSUMc) // 2;
 
    # Calculate b
    b = aSUMb - a;
 
    # Calculate c
    c = aSUMc - a;
 
    # Pra
    print("a = " , a, end = "");
 
    # Prb
    print(", b = " , b, end = "");
 
    # Prc
    print(", c = " , c, end = "");
 
 
# Driver Code
if __name__ == '__main__':
    aXORb = 30; aANDb = 0; aXORc = 20; aANDc = 10; bXORc = 10; bANDc = 20;
 
    findNumbers(aXORb, aANDb, aXORc, aANDc, bXORc, bANDc);
 
# This code contributed by shikhasingrajput


C#
// C# code for above approach
using System;
public class GFG
{
 
  // Function to find the triplet with given
  // Bitwise XOR and Bitwise AND values of all
  // possible pairs of the triplet
  static void findNumbers(int aXORb, int aANDb,
                          int aXORc, int aANDc,
                          int bXORc, int bANDc)
  {
 
    // Stores values of
    // a triplet
    int a, b, c;
 
    // Stores a + b
    int aSUMb;
 
    // Stores a + c
    int aSUMc;
 
    // Stores b + c
    int bSUMc;
 
    // Calculate aSUMb
    aSUMb = aXORb + aANDb * 2;
 
    // Calculate aSUMc
    aSUMc = aXORc + aANDc * 2;
 
    // Calculate bSUMc
    bSUMc = bXORc + bANDc * 2;
 
    // Calculate a
    a = (aSUMb - bSUMc + aSUMc) / 2;
 
    // Calculate b
    b = aSUMb - a;
 
    // Calculate c
    c = aSUMc - a;
 
    // Print a
    System.Console.Write("a = " + a);
 
    // Print b
    System.Console.Write(", b = " + b);
 
    // Print c
    System.Console.Write(", c = " + c);
  }
 
  // Driver code
  static public void Main ()
  {
    int aXORb = 30, aANDb = 0,
    aXORc = 20, aANDc = 10,
    bXORc = 10, bANDc = 20;
 
    findNumbers(aXORb, aANDb, aXORc,
                aANDc, bXORc, bANDc);
  }
}
 
// This code is contributed by offbeat.


Javascript


输出:
a = 10, b = 20, c = 30

时间复杂度: O(1)
辅助空间: O(1)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程