鉴于3个阳性整数A,B和C,该任务是在计数A和B所需要的位,使得位或A和B等于C或不翻转的的最小数目。
例子:
Input: A = 2, B = 2, C = 3
Output: 1
Explanation:
The binary representation of A is 010, B is 010 and C is 011.
Flip the 3rd bit of either A or B, such that A | B = C, i.e. 011 | 010 = 011.
Therefore, the total number of flips required is 1.
Input: A = 2, B = 6, C = 5
Output: 3
处理方法:按照以下步骤解决问题:
- 初始化一个变量,比如res ,它存储所需的最小翻转位数。
- 迭代A 、 B和C 的每一位并执行以下步骤:
- 如果C的第i个位没有设置,然后检查以下内容:
- 如果A的第i个位被设置,增量RES与1,I A的第i位需要被翻转。
- 如果B的第i个位被设置,增量RES与1,I B的第i位需要被翻转。
- 如果C的第i位设置,然后检查以下内容:
- 如果A和B 的第i位都未设置,则将res增加1 ,其中任一位都需要翻转。
- 如果A和B 的第i位都设置了,我们就不必翻转任何位。
- 如果C的第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
Python3
# Python3 program for the above approach
# Function to count the number of
# bit flips required on A and B
# such that Bitwise OR of A and B is C
def minimumFlips(A, B, C):
# Stores the count of flipped bit
res = 0
# Iterate over the range [0, 32]
for i in range(32):
x, y, z = 0, 0, 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 += 1
# Check if i-th bit of
# B is set or not
if (y):
res += 1
# 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 and y == 0):
res += 1
# Return the count
# of bits flipped
return res
# Driver Code
if __name__ == '__main__':
A, B, C = 2, 6, 5
print(minimumFlips(A, B, C))
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
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;
Console.WriteLine(minimumFlips(A, B, C));
}
}
// This code is contributed by ukasp.
Javascript
输出:
3
时间复杂度: O(1)
辅助空间: O(1)