📌  相关文章
📜  求K乘以N的N XOR’ed值

📅  最后修改于: 2021-05-04 19:19:32             🧑  作者: Mango

给定两个整数NK ,任务是找到N XOR N XOR N XOR…XOR N的值(K倍)

例子:

天真的方法:只需运行一个for循环,然后X或N,K次。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return n ^ n ^ ... k times
int xorK(int n, int k)
{
  
    // Find the result
    int res = n;
    for (int i = 1; i < k; i++)
        res = (res ^ n);
    return n;
}
  
// Driver code
int main()
{
    int n = 123, k = 3;
  
    cout << xorK(n, k);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
  
// Function to return n ^ n ^ ... k times
static int xorK(int n, int k)
{
  
    // Find the result
    int res = n;
    for (int i = 1; i < k; i++)
        res = (res ^ n);
    return n;
}
  
// Driver code
public static void main(String[] args)
{
    int n = 123, k = 3;
  
    System.out.print(xorK(n, k));
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of the approach
  
# Function to return n ^ n ^ ... k times
def xorK(n, k):
  
    # Find the result
    res = n
    for i in range(1, k):
        res = (res ^ n)
    return n
  
# Driver code
n = 123
k = 3
  
print(xorK(n, k))
  
# This code is contributed by Mohit Kumar


C#
// C# implementation of the approach
using System;
  
class GFG
{
      
// Function to return n ^ n ^ ... k times
static int xorK(int n, int k)
{
  
    // Find the result
    int res = n;
    for (int i = 1; i < k; i++)
        res = (res ^ n);
    return n;
}
  
// Driver code
static public void Main ()
{
    int n = 123, k = 3;
      
    Console.Write(xorK(n, k));
}
}
  
// This code is contributed by ajit.


C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return n ^ n ^ ... k times
int xorK(int n, int k)
{
  
    // If k is odd the answer is
    // the number itself
    if (k % 2 == 1)
        return n;
  
    // Else the answer is 0
    return 0;
}
  
// Driver code
int main()
{
    int n = 123, k = 3;
  
    cout << xorK(n, k);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
  
// Function to return n ^ n ^ ... k times
static int xorK(int n, int k)
{
  
    // If k is odd the answer is
    // the number itself
    if (k % 2 == 1)
        return n;
  
    // Else the answer is 0
    return 0;
}
  
// Driver code
public static void main(String[] args)
{
    int n = 123, k = 3;
  
    System.out.print(xorK(n, k));
}
}
  
// This code is contributed by PrinciRaj1992


Python3
# Python implementation of the approach 
  
# Function to return n ^ n ^ ... k times 
def xorK(n, k):
      
    # If k is odd the answer is 
    # the number itself 
    if (k % 2 == 1):
        return n
  
    # Else the answer is 0 
    return 0
  
# Driver code 
n = 123
k = 3
  
print(xorK(n, k))
  
# This code is contributed by Sanjit_Prasad


C#
// C# implementation of the approach
using System;
  
class GFG
{
  
// Function to return n ^ n ^ ... k times
static int xorK(int n, int k)
{
  
    // If k is odd the answer is
    // the number itself
    if (k % 2 == 1)
        return n;
  
    // Else the answer is 0
    return 0;
}
  
// Driver code
public static void Main(String[] args)
{
    int n = 123, k = 3;
  
    Console.Write(xorK(n, k));
}
}
  
// This code is contributed by 29AjayKumar


输出:
123

时间复杂度: O(K)
空间复杂度: O(1)

有效的方法:从属性X XOR X = 0X ^ 0 = X可以看出,当K为奇数时,答案将为N本身,否则答案将为0

下面是上述方法的实现:

C++

// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return n ^ n ^ ... k times
int xorK(int n, int k)
{
  
    // If k is odd the answer is
    // the number itself
    if (k % 2 == 1)
        return n;
  
    // Else the answer is 0
    return 0;
}
  
// Driver code
int main()
{
    int n = 123, k = 3;
  
    cout << xorK(n, k);
  
    return 0;
}

Java

// Java implementation of the approach
class GFG
{
  
// Function to return n ^ n ^ ... k times
static int xorK(int n, int k)
{
  
    // If k is odd the answer is
    // the number itself
    if (k % 2 == 1)
        return n;
  
    // Else the answer is 0
    return 0;
}
  
// Driver code
public static void main(String[] args)
{
    int n = 123, k = 3;
  
    System.out.print(xorK(n, k));
}
}
  
// This code is contributed by PrinciRaj1992

Python3

# Python implementation of the approach 
  
# Function to return n ^ n ^ ... k times 
def xorK(n, k):
      
    # If k is odd the answer is 
    # the number itself 
    if (k % 2 == 1):
        return n
  
    # Else the answer is 0 
    return 0
  
# Driver code 
n = 123
k = 3
  
print(xorK(n, k))
  
# This code is contributed by Sanjit_Prasad

C#

// C# implementation of the approach
using System;
  
class GFG
{
  
// Function to return n ^ n ^ ... k times
static int xorK(int n, int k)
{
  
    // If k is odd the answer is
    // the number itself
    if (k % 2 == 1)
        return n;
  
    // Else the answer is 0
    return 0;
}
  
// Driver code
public static void Main(String[] args)
{
    int n = 123, k = 3;
  
    Console.Write(xorK(n, k));
}
}
  
// This code is contributed by 29AjayKumar
输出:
123

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