# 用所有其他的BitWise XOR替换数组的每个元素

2021年4月22日15:00:36 发表评论 497 次浏览

## 本文概述

``````Input: arr[] = { 2, 3, 3, 5, 5 }
Output: 0 1 1 7 7
Bitwise Xor of 3, 3, 5, 5 = 0
Bitwise Xor of 2, 3, 5, 5 = 1
Bitwise Xor of 2, 3, 5, 5 = 1
Bitwise Xor of 2, 3, 3, 5 = 7
Bitwise Xor of 2, 3, 3, 5 = 7

Input : arr[] = { 1, 2, 3 }
Output : 1 2 3``````

1. 首先, 对数组所有元素进行按位异或, 并将其存储在变量let X中。
2. 现在, 将每个元素替换为X和该元素的异或, 因为对同一个元素进行异或将抵消所有其他元素的异或。
3. 打印修改后的数组。

## C ++

``````//C++ program to Replace every element
//by the bitwise xor of all other elements
#include <bits/stdc++.h>
using namespace std;

//Function to replace the elements
void ReplaceElements( int arr[], int n)
{
int X = 0;

//Calculate the xor of all the elements
for ( int i = 0; i <n; ++i) {
X ^= arr[i];
}

//Replace every element by the
//xor of all other elements
for ( int i = 0; i <n; ++i) {
arr[i] = X ^ arr[i];
}
}

//Driver code
int main()
{
int arr[] = { 2, 3, 3, 5, 5 };
int n = sizeof (arr) /sizeof (arr[0]);

ReplaceElements(arr, n);

//Print the modified array.
for ( int i = 0; i <n; ++i) {
cout <<arr[i] <<" " ;
}
return 0;
}``````

## Java

``````//Java  program to Replace every element
//by the bitwise xor of all other elements

import java.io.*;

class GFG {

//Function to replace the elements
static void ReplaceElements( int arr[], int n)
{
int X = 0 ;

//Calculate the xor of all the elements
for ( int i = 0 ; i <n; ++i) {
X ^= arr[i];
}

//Replace every element by the
//xor of all other elements
for ( int i = 0 ; i <n; ++i) {
arr[i] = X ^ arr[i];
}
}

//Driver code
public static void main (String[] args) {

int arr[] = { 2 , 3 , 3 , 5 , 5 };
int n = arr.length;

ReplaceElements(arr, n);

//Print the modified array.
for ( int i = 0 ; i <n; ++i) {
System.out.print(arr[i] + " " );

}
}
}``````

## Python 3

``````# Python 3 program to Replace every element
# by the bitwise xor of all other elements

# Function to replace the elements
def ReplaceElements(arr, n):

X = 0

# Calculate the xor of all the elements
for i in range (n):
X ^ = arr[i]

# Replace every element by the
# xor of all other elements
for i in range (n):
arr[i] = X ^ arr[i]

# Driver code
if __name__ = = "__main__" :
arr = [ 2 , 3 , 3 , 5 , 5 ]
n = len (arr)

ReplaceElements(arr, n)

# Print the modified array.
for i in range (n):
print (arr[i], end = " " )

# This code is contributed
# by ChitraNayal``````

## C#

``````//C#  program to Replace every element
//by the bitwise xor of all other elements
using System;

public class GFG{
//Function to replace the elements
static void ReplaceElements( int []arr, int n)
{
int X = 0;

//Calculate the xor of all the elements
for ( int i = 0; i <n; ++i) {
X ^= arr[i];
}

//Replace every element by the
//xor of all other elements
for ( int i = 0; i <n; ++i) {
arr[i] = X ^ arr[i];
}
}

//Driver code

static public void Main (){

int []arr = { 2, 3, 3, 5, 5 };
int n = arr.Length;

ReplaceElements(arr, n);

//Print the modified array.
for ( int i = 0; i <n; ++i) {
Console.Write(arr[i] + " " );

}
}
//This code is contributed by ajit
}``````

## 的PHP

``````<?php
//PHP program to Replace every element
//by the bitwise xor of all other elements

//Function to replace the elements
function ReplaceElements( \$arr , \$n )
{
\$X = 0;

//Calculate the xor of all the elements
for ( \$i = 0; \$i <\$n ; ++ \$i )
{
\$X ^= \$arr [ \$i ];
}

//Replace every element by the
//xor of all other elements
for ( \$i = 0; \$i <\$n ; ++ \$i )
{
\$arr [ \$i ] = \$X ^ \$arr [ \$i ];
}
return \$arr ;
}

//Driver code
\$arr = array ( 2, 3, 3, 5, 5 );
\$n = sizeof( \$arr );

\$arr1 = ReplaceElements( \$arr , \$n );

//Print the modified array.
for ( \$i = 0; \$i <\$n ; ++ \$i )
{
echo ( \$arr1 [ \$i ] . " " );

}

//This code is contributed
//by Mukul singh
?>``````

``0 1 1 7 7``