给定尺寸为M * N的矩阵arr [] [] ,任务是将每个矩阵元素转换为元素中存在的数字的按位XOR。
例子:
Input: arr[][] = {{27, 173}, {5, 21}}
Output:
5 5
5 3
Explanation:
Bitwise XOR of digits of arr[0][0] (= 27) is 5 (2^7).
Bitwise XOR value of digits of arr[0][1] (= 173) is 5 (1 ^ 7 ^ 3).
Bitwise XOR value of digits of arr[1][0] (= 5) is 5.
Bitwise XOR value of digits of arr[1][1] (= 21) is 3(1 ^ 2).
Input: arr[][] = {{11, 12, 33}, {64, 57, 61}, {74, 88, 39}}
Output:
0 3 0
2 2 7
3 0 10
方法:解决此问题的方法思想是遍历给定的矩阵,并为每个矩阵元素打印其数字的按位XOR。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
const int M = 3;
const int N = 3;
// Function to calculate Bitwise
// XOR of digits present in X
int findXOR(int X)
{
// Stores the Bitwise XOR
int ans = 0;
// While X is true
while (X) {
// Update Bitwise
// XOR of its digits
ans ^= (X % 10);
X /= 10;
}
// Return the result
return ans;
}
// Function to print matrix after
// converting each matrix element
// to XOR of its digits
void printXORmatrix(int arr[M][N])
{
// Traverse each row of arr[][]
for (int i = 0; i < M; i++) {
// Traverse each column of arr[][]
for (int j = 0; j < N; j++) {
cout << arr[i][j] << " ";
}
cout << "\n";
}
}
// Function to convert the given
// matrix to required XOR matrix
void convertXOR(int arr[M][N])
{
// Traverse each row of arr[][]
for (int i = 0; i < M; i++) {
// Traverse each column of arr[][]
for (int j = 0; j < N; j++) {
// Store the current
// matrix element
int X = arr[i][j];
// Find the xor of
// digits present in X
int temp = findXOR(X);
// Stores the XOR value
arr[i][j] = temp;
}
}
// Print resultant matrix
printXORmatrix(arr);
}
// Driver Code
int main()
{
int arr[][3] = { { 27, 173, 5 },
{ 21, 6, 624 },
{ 5, 321, 49 } };
convertXOR(arr);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG{
static int M = 3;
static int N = 3;
// Function to calculate Bitwise
// XOR of digits present in X
static int findXOR(int X)
{
// Stores the Bitwise XOR
int ans = 0;
// While X is true
while (X != 0)
{
// Update Bitwise
// XOR of its digits
ans ^= (X % 10);
X /= 10;
}
// Return the result
return ans;
}
// Function to print matrix after
// converting each matrix element
// to XOR of its digits
static void printXORmatrix(int arr[][])
{
// Traverse each row of arr[][]
for(int i = 0; i < M; i++)
{
// Traverse each column of arr[][]
for(int j = 0; j < N; j++)
{
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
// Function to convert the given
// matrix to required XOR matrix
static void convertXOR(int arr[][])
{
// Traverse each row of arr[][]
for(int i = 0; i < M; i++)
{
// Traverse each column of arr[][]
for(int j = 0; j < N; j++)
{
// Store the current
// matrix element
int X = arr[i][j];
// Find the xor of
// digits present in X
int temp = findXOR(X);
// Stores the XOR value
arr[i][j] = temp;
}
}
// Print resultant matrix
printXORmatrix(arr);
}
// Driver Code
public static void main (String[] args)
{
int arr[][] = { { 27, 173, 5 },
{ 21, 6, 624 },
{ 5, 321, 49 } };
convertXOR(arr);
}
}
// This code is contributed by sanjoy_62
Python3
# Python3 program for the above approach
M = 3
N = 3
# Function to calculate Bitwise
# XOR of digits present in X
def findXOR(X):
# Stores the Bitwise XOR
ans = 0
# While X is true
while (X):
# Update Bitwise
# XOR of its digits
ans ^= (X % 10)
X //= 10
# Return the result
return ans
# Function to prmatrix after
# converting each matrix element
# to XOR of its digits
def printXORmatrix(arr):
# Traverse each row of arr[][]
for i in range(3):
# Traverse each column of arr[][]
for j in range(3):
print(arr[i][j], end = " ")
print()
# Function to convert the given
# matrix to required XOR matrix
def convertXOR(arr):
# Traverse each row of arr[][]
for i in range(3):
# Traverse each column of arr[][]
for j in range(3):
# Store the current
# matrix element
X = arr[i][j]
# Find the xor of
# digits present in X
temp = findXOR(X)
# Stores the XOR value
arr[i][j] = temp
# Prresultant matrix
printXORmatrix(arr)
# Driver Code
if __name__ == '__main__':
arr=[[27, 173, 5],
[ 21, 6, 624 ],
[ 5, 321, 49 ]]
convertXOR(arr)
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
static int M = 3;
static int N = 3;
// Function to calculate Bitwise
// XOR of digits present in X
static int findXOR(int X)
{
// Stores the Bitwise XOR
int ans = 0;
// While X is true
while (X != 0)
{
// Update Bitwise
// XOR of its digits
ans ^= (X % 10);
X /= 10;
}
// Return the result
return ans;
}
// Function to print matrix after
// converting each matrix element
// to XOR of its digits
static void printXORmatrix(int[,] arr)
{
// Traverse each row of arr[][]
for(int i = 0; i < M; i++)
{
// Traverse each column of arr[][]
for(int j = 0; j < N; j++)
{
Console.Write(arr[i, j] + " ");
}
Console.WriteLine();
}
}
// Function to convert the given
// matrix to required XOR matrix
static void convertXOR(int[,] arr)
{
// Traverse each row of arr[][]
for(int i = 0; i < M; i++)
{
// Traverse each column of arr[][]
for(int j = 0; j < N; j++)
{
// Store the current
// matrix element
int X = arr[i, j];
// Find the xor of
// digits present in X
int temp = findXOR(X);
// Stores the XOR value
arr[i, j] = temp;
}
}
// Print resultant matrix
printXORmatrix(arr);
}
// Driver Code
static public void Main()
{
int[,] arr = { { 27, 173, 5 },
{ 21, 6, 624 },
{ 5, 321, 49 } };
convertXOR(arr);
}
}
// This code is contributed by splevel62
输出:
5 5 5
3 6 0
5 0 13
时间复杂度: O(M * N * log 10 K),其中K是矩阵中存在的最大元素。
辅助空间: O(1)