📌  相关文章
📜  要求翻转的最小位数,以使A和B的按位或等于C

📅  最后修改于: 2021-04-27 09:36:37             🧑  作者: Mango

鉴于3个阳性整数A,BC,该任务是在计数AB所需要的位,使得位或AB等于C或不翻转的的最小数目。

例子:

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

  • 初始化一个变量,例如res ,该变量存储所需的最少翻转位数。
  • 遍历ABC的一位,然后执行以下步骤:
    • 如果Ci位没有设置,然后检查以下内容:
      • 如果Ai位被设置,增量RES1,I A第i位需要被翻转。
      • 如果Bi位被设置,增量RES1,I B第i位需要被翻转。
    • 如果C第i位设置,然后检查以下内容:
      • 如果未同时设置AB的i位,则将res增大1 ,则任一位都需要翻转。
      • 如果同时设置了AB的i位,则无需翻转任何位。
  • 完成上述步骤后,打印res的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count the number of
// bit flips required on A and B
// such that Bitwise OR of A and B is C
int minimumFlips(int A, int B, int C)
{
    // Stores the count of flipped bit
    int res = 0;
 
    // Iterate over the range [0, 32]
    for (int i = 0; i < 32; i++) {
 
        int x = 0, y = 0, z = 0;
 
        // Check if i-th bit of A is set
        if (A & (1 << i)) {
            x = 1;
        }
 
        // Check if i-th bit of B is
        // set or not
        if (B & (1 << i)) {
            y = 1;
        }
 
        // Check if i-th bit of C is
        // set or not
        if (C & (1 << i)) {
            z = 1;
        }
 
        // If i-th bit of C is unset
        if (z == 0) {
 
            // Check if i-th bit of
            // A is set or not
            if (x) {
                res++;
            }
 
            // Check if i-th bit of
            // B is set or not
            if (y) {
                res++;
            }
        }
 
        // Check if i-th bit of C
        // is set or not
        if (z == 1) {
 
            // Check if i-th bit of
            // both A and B is set
            if (x == 0 && y == 0) {
                res++;
            }
        }
    }
 
    // Return the count
    // of bits flipped
    return res;
}
 
// Driver Code
int main()
{
    int A = 2, B = 6, C = 5;
    cout << minimumFlips(A, B, C);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG{
 
// Function to count the number of
// bit flips required on A and B
// such that Bitwise OR of A and B is C
static int minimumFlips(int A, int B, int C)
{
     
    // Stores the count of flipped bit
    int res = 0;
 
    // Iterate over the range [0, 32]
    for(int i = 0; i < 32; i++)
    {
        int x = 0, y = 0, z = 0;
 
        // Check if i-th bit of A is set
        if ((A & (1 << i)) != 0)
        {
            x = 1;
        }
 
        // Check if i-th bit of B is
        // set or not
        if ((B & (1 << i)) != 0)
        {
            y = 1;
        }
 
        // Check if i-th bit of C is
        // set or not
        if ((C & (1 << i)) != 0)
        {
            z = 1;
        }
 
        // If i-th bit of C is unset
        if (z == 0)
        {
 
            // Check if i-th bit of
            // A is set or not
            if (x == 1)
            {
                res++;
            }
 
            // Check if i-th bit of
            // B is set or not
            if (y == 1)
            {
                res++;
            }
        }
 
        // Check if i-th bit of C
        // is set or not
        if (z == 1)
        {
 
            // Check if i-th bit of
            // both A and B is set
            if (x == 0 && y == 0)
            {
                res++;
            }
        }
    }
 
    // Return the count
    // of bits flipped
    return res;
}
 
// Driver Code
public static void main(String[] args)
{
    int A = 2, B = 6, C = 5;
     
    System.out.println(minimumFlips(A, B, C));
}
}
 
// This code is contributed by Kingash


输出:
3

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