📌  相关文章
📜  最小化要在X和Y中翻转的位,以使它们的按位或等于Z

📅  最后修改于: 2021-05-25 04:06:01             🧑  作者: Mango

给定三个正整数XYZ ,任务是计算在XY中需要翻转的最小位数,以使X OR Y(X | Y)等于Z。
例子:

Input : X = 5   Y = 8   Z = 6
Output : 3
Explanation : 
        Before          After 
        Flipping        Flipping 
X   ::  0101            0100 
Y   ::  1000            0010
       ------           ----- 
Z   ::  0110            0110
So we need to flip 3 bits in order 
to make (X | Y) = Z .

Input : X = 1   Y = 2   Z = 3
Output : 0

方法 :
为了解决此问题,我们需要观察到仅在以下情况下才需要翻转X或Y:

  • 如果设置了Z中的当前位,而未同时设置X和Y中的相应位,则需要在X或Y中设置一个位。
  • 如果未设置Z中的当前位,则需要取消设置A和B中的相应位(以设置为准)。如果两者都设置,则同时取消设置。

因此,我们需要遍历X,Y和Z的位,并按照上述两个步骤来获得所需的结果。
下面是上述方法的实现:

C++
// C++ Program to minimize
// number of bits to be fipped
// in X or Y such that their
// bitwise or is equal to minimize
 
#include 
using namespace std;
 
// This function returns minimum
// number of bits to be flipped in
// X and Y to make X | Y = Z
int minimumFlips(int X, int Y, int Z)
{
    int res = 0;
    while (X > 0 || Y > 0 || Z > 0) {
         
        // If current bit in Z is set and is
        // also set in either of X or Y or both
        if (((X & 1) || (Y & 1)) && (Z & 1)) {
            X = X >> 1;
            Y = Y >> 1;
            Z = Z >> 1;
            continue;
        }
        // If current bit in Z is set
        // and is unset in both X and Y
        else if (!(X & 1) && !(Y & 1) && (Z & 1)) {
            // Set that bit in either X or Y
            res++;
        }
 
        else if ((X & 1) || (Y & 1) == 1)
        {
            // If current bit in Z is unset
            // and is set in both X and Y
            if ((X & 1) && (Y & 1) && !(Z & 1)) {
                // Unset the bit in both X and Y
                res += 2;
            }
            // If current bit in Z is unset and
            // is set in either X or Y
            else if (((X & 1) || (Y & 1)) && !(Z & 1))
            {
                // Unset that set bit
                res++;
            }
        }
        X = X >> 1;
        Y = Y >> 1;
        Z = Z >> 1;
    }
    return res;
}
 
// Driver Code
int main()
{
    int X = 5, Y = 8, Z = 6;
    cout << minimumFlips(X, Y, Z);
    return 0;
}


Java
// Java program to minimize
// number of bits to be fipped
// in X or Y such that their
// bitwise or is equal to minimize
import java.util.*;
 
class GFG{
 
// This function returns minimum
// number of bits to be flipped in
// X and Y to make X | Y = Z
static int minimumFlips(int X, int Y, int Z)
{
    int res = 0;
    while (X > 0 || Y > 0 || Z > 0)
    {
         
        // If current bit in Z is set and is
        // also set in either of X or Y or both
        if (((X % 2 == 1) ||
             (Y % 2 == 1)) &&
             (Z % 2 == 1))
        {
            X = X >> 1;
            Y = Y >> 1;
            Z = Z >> 1;
            continue;
        }
         
        // If current bit in Z is set
        // and is unset in both X and Y
        else if (!(X % 2 == 1) &&
                 !(Y % 2 == 1) &&
                  (Z % 2 == 1))
        {
             
            // Set that bit in either X or Y
            res++;
        }
 
        else if ((X % 2 == 1) || (Y % 2 == 1))
        {
             
            // If current bit in Z is unset
            // and is set in both X and Y
            if ((X % 2 == 1) &&
                (Y % 2 == 1) &&
               !(Z % 2 == 1))
            {
                 
                // Unset the bit in both X and Y
                res += 2;
            }
             
            // If current bit in Z is unset and
            // is set in either X or Y
            else if (((X % 2 == 1) ||
                      (Y % 2 == 1)) &&
                     !(Z % 2 == 1))
            {
                 
                // Unset that set bit
                res++;
            }
        }
        X = X >> 1;
        Y = Y >> 1;
        Z = Z >> 1;
    }
    return res;
}
 
// Driver Code
public static void main(String[] args)
{
    int X = 5, Y = 8, Z = 6;
     
    System.out.print(minimumFlips(X, Y, Z));
}
}
 
// This code is contributed by amal kumar choubey


Python3
# Python 3 Program to minimize
# number of bits to be fipped
# in X or Y such that their
# bitwise or is equal to minimize
 
# This function returns minimum
# number of bits to be flipped in
# X and Y to make X | Y = Z
def minimumFlips(X, Y, Z):
 
    res = 0
    while (X > 0 or Y > 0 or
           Z > 0):
 
        # If current bit in Z is
        # set and is also set in
        # either of X or Y or both
        if (((X & 1) or (Y & 1)) and
             (Z & 1)):
            X = X >> 1
            Y = Y >> 1
            Z = Z >> 1
            continue
 
        # If current bit in Z is set
        # and is unset in both X and Y
        elif (not (X & 1) and not(Y & 1) and
                  (Z & 1)):
           
            # Set that bit in either
            # X or Y
            res += 1
 
        elif ((X & 1) or (Y & 1) == 1):
 
            # If current bit in Z is unset
            # and is set in both X and Y
            if ((X & 1) and (Y & 1) and
                 not(Z & 1)):
               
                # Unset the bit in both
                # X and Y
                res += 2
 
            # If current bit in Z is
            # unset and is set in
            # either X or Y
            elif (((X & 1) or (Y & 1)) and
                    not(Z & 1)):
 
                # Unset that set bit
                res += 1
 
        X = X >> 1
        Y = Y >> 1
        Z = Z >> 1
 
    return res
 
# Driver Code
if __name__ == "__main__":
 
    X = 5
    Y = 8
    Z = 6
    print(minimumFlips(X, Y, Z))
 
# This code is contributed by Chitranayal


C#
// C# program to minimize
// number of bits to be fipped
// in X or Y such that their
// bitwise or is equal to minimize
using System;
class GFG{
 
// This function returns minimum
// number of bits to be flipped in
// X and Y to make X | Y = Z
static int minimumFlips(int X, int Y, int Z)
{
    int res = 0;
    while (X > 0 || Y > 0 || Z > 0)
    {
         
        // If current bit in Z is set and is
        // also set in either of X or Y or both
        if (((X % 2 == 1) ||
             (Y % 2 == 1)) &&
             (Z % 2 == 1))
        {
            X = X >> 1;
            Y = Y >> 1;
            Z = Z >> 1;
            continue;
        }
         
        // If current bit in Z is set
        // and is unset in both X and Y
        else if (!(X % 2 == 1) &&
                 !(Y % 2 == 1) &&
                  (Z % 2 == 1))
        {
             
            // Set that bit in either X or Y
            res++;
        }
 
        else if ((X % 2 == 1) || (Y % 2 == 1))
        {
             
            // If current bit in Z is unset
            // and is set in both X and Y
            if ((X % 2 == 1) &&
                (Y % 2 == 1) &&
               !(Z % 2 == 1))
            {
                 
                // Unset the bit in both X and Y
                res += 2;
            }
             
            // If current bit in Z is unset and
            // is set in either X or Y
            else if (((X % 2 == 1) ||
                      (Y % 2 == 1)) &&
                     !(Z % 2 == 1))
            {
                 
                // Unset that set bit
                res++;
            }
        }
        X = X >> 1;
        Y = Y >> 1;
        Z = Z >> 1;
    }
    return res;
}
 
// Driver Code
public static void Main()
{
    int X = 5, Y = 8, Z = 6;
     
    Console.Write(minimumFlips(X, Y, Z));
}
}
 
// This code is contributed by Code_Mech


Javascript


输出:
3

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