📜  等于和与异或

📅  最后修改于: 2021-05-06 07:45:33             🧑  作者: Mango

给定一个正整数n,找到正整数i的计数,使得0 <= i <= n且n + i = n ^ i

例子 :

Input  : n = 7
Output : 1
Explanation:
7^i = 7+i holds only for only for i = 0
7+0 = 7^0 = 7

Input: n = 12
Output: 4
12^i = 12+i hold only for i = 0, 1, 2, 3
for i=0, 12+0 = 12^0 = 12
for i=1, 12+1 = 12^1 = 13
for i=2, 12+2 = 12^2 = 14
for i=3, 12+3 = 12^3 = 15

方法1(简单):
一种简单的解决方案是遍历i 0 <= i <= n的所有值并计算所有满足的值。

C++
/* C++ program to print count of values such
   that n+i = n^i */
#include 
using namespace std;
 
// function to count number of values less than
// equal to n that satisfy the given condition
int countValues (int n)
{
    int countV = 0;
 
    // Traverse all numbers from 0 to n and
    // increment result only when given condition
    // is satisfied.
    for (int i=0; i<=n; i++ )
        if ((n+i) == (n^i) )
            countV++;
 
    return countV;
}
 
// Driver program
int main()
{
    int n = 12;
    cout << countValues(n);
    return 0;
}


Java
/* Java program to print count of values
 such that n+i = n^i */
import java.util.*;
 
class GFG {
     
    // function to count number of values
    // less than equal to n that satisfy
    // the given condition
    public static int countValues (int n)
    {
        int countV = 0;
      
        // Traverse all numbers from 0 to n
        // and increment result only when
        // given condition is satisfied.
        for (int i = 0; i <= n; i++ )
            if ((n + i) == (n ^ i) )
                countV++;
      
        return countV;
    }
     
    /* Driver program to test above function */
    public static void main(String[] args)
    {
        int n = 12;
        System.out.println(countValues(n));
         
    }
}
 
// This code is contributed by Arnav Kr. Mandal.


Python3
# Python3 program to print count
# of values such that n+i = n^i
 
# function to count number
# of values less than
# equal to n that satisfy
# the given condition
def countValues (n):
    countV = 0;
 
    # Traverse all numbers
    # from 0 to n and
    # increment result only
    # when given condition
    # is satisfied.
    for i in range(n + 1):
        if ((n + i) == (n ^ i)):
            countV += 1;
 
    return countV;
 
# Driver Code
n = 12;
print(countValues(n));
 
# This code is contributed by mits


C#
/* C# program to print count of values
such that n+i = n^i */
using System;
 
class GFG {
     
    // function to count number of values
    // less than equal to n that satisfy
    // the given condition
    public static int countValues (int n)
    {
        int countV = 0;
     
        // Traverse all numbers from 0 to n
        // and increment result only when
        // given condition is satisfied.
        for (int i = 0; i <= n; i++ )
            if ((n + i) == (n ^ i) )
                countV++;
     
        return countV;
    }
     
    /* Driver program to test above function */
    public static void Main()
    {
        int n = 12;
        Console.WriteLine(countValues(n));
         
    }
}
 
// This code is contributed by anuj_67.


PHP


Javascript


C++
/* c++ program to print count of values such
  that n+i = n^i */
#include 
using namespace std;
 
// function to count number of values less than
// equal to n that satisfy the given condition
int countValues(int n)
{
    // unset_bits keeps track of count of un-set
    // bits in binary representation of n
    int unset_bits=0;
    while (n)
    {
        if ((n & 1) == 0)
            unset_bits++;
        n=n>>1;
    }
 
    // Return 2 ^ unset_bits
    return 1 << unset_bits;
}
 
// Driver code
int main()
{
    int n = 12;
    cout << countValues(n);
    return 0;
}


Java
/* Java program to print count of values
  such that n+i = n^i */
import java.util.*;
 
class GFG {
     
    // function to count number of values
    // less than equal to n that satisfy
    // the given condition
    public static int countValues(int n)
    {
        // unset_bits keeps track of count
        // of un-set bits in binary
        // representation of n
        int unset_bits=0;
        while (n > 0)
        {
            if ((n & 1) == 0)
                unset_bits++;
            n=n>>1;
        }
      
        // Return 2 ^ unset_bits
        return 1 << unset_bits;
    }
     
    /* Driver program to test above
    function */
    public static void main(String[] args)
    {
        int n = 12;
        System.out.println(countValues(n));
           
    }
}
   
// This code is contributed by Arnav Kr. Mandal.


Python3
# Python3 program to print count of values such
# that n+i = n^i
 
# function to count number of values less than
# equal to n that satisfy the given condition
def countValues(n):
     
    # unset_bits keeps track of count of un-set
    # bits in binary representation of n
    unset_bits = 0
     
    while(n):
        if n & 1 == 0:
            unset_bits += 1
        n = n >> 1
         
    # Return 2 ^ unset_bits    
    return 1 << unset_bits
 
# Driver code
if __name__=='__main__':
    n = 12
    print(countValues(n))
 
# This code is contributed by rutvik


PHP
> 1;
    }
 
    // Return 2 ^ unset_bits
    return 1 << $unset_bits;
}
 
// Driver code
 
    $n = 12;
    echo countValues($n);
 
// This code is contributed
// by Anuj_67.
?>


Javascript


输出:

4

方法2(高效):
一个有效的解决方案如下

我们知道(n + i)=(n ^ i)+ 2 *(n&i)
所以n + i = n ^ i意味着n&i = 0
因此,我们的问题简化为找到i的值,使得n&i =0。如何找到此类对的数量?我们可以在n的二进制表示中使用未置位的计数。为了使n&i为零,我必须取消设置n的所有置位。如果第k位设置为n中的特定值,则i中的k位必须始终为0,否则i的k位可以为0或1
因此,此类组合的总数为2 ^(n中未设置的位数)
例如,考虑n = 12(二进制表示形式:1 1 0 0)。
可以取消设置n的所有位的i的所有可能值为0 0 0/1 0/1,其中0/1表示0或1。i的此类值的数量为2 ^ 2 = 4。

以下是遵循上述思想的程序。

C++

/* c++ program to print count of values such
  that n+i = n^i */
#include 
using namespace std;
 
// function to count number of values less than
// equal to n that satisfy the given condition
int countValues(int n)
{
    // unset_bits keeps track of count of un-set
    // bits in binary representation of n
    int unset_bits=0;
    while (n)
    {
        if ((n & 1) == 0)
            unset_bits++;
        n=n>>1;
    }
 
    // Return 2 ^ unset_bits
    return 1 << unset_bits;
}
 
// Driver code
int main()
{
    int n = 12;
    cout << countValues(n);
    return 0;
}

Java

/* Java program to print count of values
  such that n+i = n^i */
import java.util.*;
 
class GFG {
     
    // function to count number of values
    // less than equal to n that satisfy
    // the given condition
    public static int countValues(int n)
    {
        // unset_bits keeps track of count
        // of un-set bits in binary
        // representation of n
        int unset_bits=0;
        while (n > 0)
        {
            if ((n & 1) == 0)
                unset_bits++;
            n=n>>1;
        }
      
        // Return 2 ^ unset_bits
        return 1 << unset_bits;
    }
     
    /* Driver program to test above
    function */
    public static void main(String[] args)
    {
        int n = 12;
        System.out.println(countValues(n));
           
    }
}
   
// This code is contributed by Arnav Kr. Mandal.

Python3

# Python3 program to print count of values such
# that n+i = n^i
 
# function to count number of values less than
# equal to n that satisfy the given condition
def countValues(n):
     
    # unset_bits keeps track of count of un-set
    # bits in binary representation of n
    unset_bits = 0
     
    while(n):
        if n & 1 == 0:
            unset_bits += 1
        n = n >> 1
         
    # Return 2 ^ unset_bits    
    return 1 << unset_bits
 
# Driver code
if __name__=='__main__':
    n = 12
    print(countValues(n))
 
# This code is contributed by rutvik

的PHP

> 1;
    }
 
    // Return 2 ^ unset_bits
    return 1 << $unset_bits;
}
 
// Driver code
 
    $n = 12;
    echo countValues($n);
 
// This code is contributed
// by Anuj_67.
?>

Java脚本


输出 :

4