📌  相关文章
📜  通过将每个元素转换为数字的XOR来修改矩阵

📅  最后修改于: 2021-04-17 17:26:13             🧑  作者: Mango

给定尺寸为M * N的矩阵arr [] [] ,任务是将每个矩阵元素转换为元素中存在的数字的按位XOR。

例子:

方法:解决此问题的方法思想是遍历给定的矩阵,并为每个矩阵元素打印其数字的按位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)