问题是要找到所有子集的XOR。即,如果集合是{1,2,3}。所有子集是:[{1},{2},{3},{1、2},{1、3},{2、3},{1、2、3}。找到每个子集的XOR,然后找到每个子集结果的XOR。
强烈建议您最小化浏览器,然后自己尝试。
如果我们正确地迈出了第一步(也是唯一一步),这是一个非常简单的问题。解决方案是,当n> 1时XOR始终为0,而当n为1时Set [0]总是。
C++
// C++ program to find XOR of XOR's of all subsets
#include
using namespace std;
// Returns XOR of all XOR's of given subset
int findXOR(int Set[], int n)
{
// XOR is 1 only when n is 1, else 0
if (n == 1)
return Set[0];
else
return 0;
}
// Driver program
int main()
{
int Set[] = {1, 2, 3};
int n = sizeof(Set)/sizeof(Set[0]);
cout << "XOR of XOR's of all subsets is "
<< findXOR(Set, n);
return 0;
}
Java
// Java program to find XOR of
// XOR's of all subsets
import java.util.*;
class GFG {
// Returns XOR of all XOR's of given subset
static int findXOR(int Set[], int n) {
// XOR is 1 only when n is 1, else 0
if (n == 1)
return Set[0];
else
return 0;
}
// Driver code
public static void main(String arg[])
{
int Set[] = {1, 2, 3};
int n = Set.length;
System.out.print("XOR of XOR's of all subsets is " +
findXOR(Set, n));
}
}
// This code is contributed by Anant Agarwal.
Python3
# Python program to find
# XOR of XOR's of all subsets
# Returns XOR of all
# XOR's of given subset
def findXOR(Set, n):
# XOR is 1 only when
# n is 1, else 0
if (n == 1):
return Set[0]
else:
return 0
# Driver code
Set = [1, 2, 3]
n = len(Set)
print("XOR of XOR's of all subsets is ",
findXOR(Set, n));
# This code is contributed
# by Anant Agarwal.
C#
// C# program to find XOR of
// XOR's of all subsets
using System;
class GFG {
// Returns XOR of all
// XOR's of given subset
static int findXOR(int []Set, int n)
{
// XOR is 1 only when n
// is 1, else 0
if (n == 1)
return Set[0];
else
return 0;
}
// Driver code
public static void Main()
{
int []Set = {1, 2, 3};
int n = Set.Length;
Console.Write("XOR of XOR's of all subsets is " +
findXOR(Set, n));
}
}
// This code is contributed by nitin mittal
PHP
Javascript
输出:
XOR of XOR's of all subsets is 0