交替反转矩阵的行和列
给定一个大小为M*N的矩阵arr[][] ,其中M是行数, N是列数。任务是交替反转矩阵的行和列,即先反转第一行,然后反转第二列,依此类推。
例子:
Input: arr[][] = {
{3, 4, 1, 8},
{11, 23, 43, 21},
{12, 17, 65, 91},
{71, 56, 34, 24}
}
Output: {
{8, 56, 4, 24},
{11, 17, 43, 12},
{91, 65, 23, 21},
{71, 1, 34, 3}
}
Explanation: Operations to be followed:
- Reverse the first row
- Reverse the second column
- Reverse the third row
- Reverse the fourth row
Input: { {11, 23, 43, 21}, {12, 17, 65, 91}, {71, 56, 34, 24} }
Output: { {21, 56, 23, 71}, {12, 17, 65, 91}, {24, 34, 43, 11} }
方法:该任务可以通过简单地运行两个while循环来交替遍历行和列来解决。最后,打印结果矩阵。
下面是上述方法的实现:
C++
// C++ program to find Reverse the
// rows and columns of a matrix alternatively.
#include
using namespace std;
const int N = 4;
const int M = 4;
// Print matrix elements
void showArray(int arr[][N])
{
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++)
cout << arr[i][j] << " ";
cout << endl;
}
}
// Function to Reverse the rows and columns
// of a matrix alternatively.
void reverseAlternate(int arr[][N])
{
int turn = 0;
while (turn < M && turn < N) {
if (turn % 2 == 0) {
int start = 0, end = N - 1, temp;
while (start < end) {
temp = arr[turn][start];
arr[turn][start] = arr[turn][end];
arr[turn][end] = temp;
start += 1;
end -= 1;
}
turn += 1;
}
if (turn % 2 == 1) {
int start = 0, end = M - 1, temp;
while (start < end) {
temp = arr[start][turn];
arr[start][turn] = arr[end][turn];
arr[end][turn] = temp;
start += 1;
end -= 1;
}
turn += 1;
}
}
}
// Driver code
int main()
{
int matrix[][N] = { { 3, 4, 1, 8 },
{ 11, 23, 43, 21 },
{ 12, 17, 65, 91 },
{ 71, 56, 34, 24 } };
reverseAlternate(matrix);
showArray(matrix);
}
Java
// Java program for the above approach
import java.util.*;
public class GFG
{
static int N = 4;
static int M = 4;
// Print matrix elements
static void showArray(int arr[][])
{
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++)
System.out.print(arr[i][j] + " ");
System.out.println();
}
}
// Function to Reverse the rows and columns
// of a matrix alternatively.
static void reverseAlternate(int arr[][])
{
int turn = 0;
while (turn < M && turn < N) {
if (turn % 2 == 0) {
int start = 0, end = N - 1, temp;
while (start < end) {
temp = arr[turn][start];
arr[turn][start] = arr[turn][end];
arr[turn][end] = temp;
start += 1;
end -= 1;
}
turn += 1;
}
if (turn % 2 == 1) {
int start = 0, end = M - 1, temp;
while (start < end) {
temp = arr[start][turn];
arr[start][turn] = arr[end][turn];
arr[end][turn] = temp;
start += 1;
end -= 1;
}
turn += 1;
}
}
}
// Driver Code
public static void main(String args[])
{
int matrix[][] = { { 3, 4, 1, 8 },
{ 11, 23, 43, 21 },
{ 12, 17, 65, 91 },
{ 71, 56, 34, 24 } };
reverseAlternate(matrix);
showArray(matrix);
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# Python3 program to find Reverse the
# rows and columns of a matrix alternatively.
N = 4
M = 4
# Print matrix elements
def showArray(arr):
for i in range(M):
for j in range(N):
print(arr[i][j], end = " ")
print()
# Function to Reverse the rows and columns
# of a matrix alternatively.
def reverseAlternate(arr):
turn = 0
while turn < M and turn < N:
if (turn % 2 == 0):
start = 0
end = N - 1
while (start < end):
temp = arr[turn][start]
arr[turn][start] = arr[turn][end]
arr[turn][end] = temp
start += 1
end -= 1
turn += 1
if (turn % 2 == 1):
start = 0
end = M - 1
while (start < end):
temp = arr[start][turn]
arr[start][turn] = arr[end][turn]
arr[end][turn] = temp
start += 1
end -= 1
turn += 1
# Driver code
matrix = [ [ 3, 4, 1, 8 ],
[ 11, 23, 43, 21 ],
[ 12, 17, 65, 91 ],
[ 71, 56, 34, 24 ] ]
reverseAlternate(matrix)
showArray(matrix)
# This code is contributed by Potta Lokesh
C#
// C# program to find Reverse the
// rows and columns of a matrix alternatively.
using System;
class GFG {
const int N = 4;
const int M = 4;
// Print matrix elements
static void showArray(int[, ] arr)
{
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++)
Console.Write(arr[i, j] + " ");
Console.WriteLine();
}
}
// Function to Reverse the rows and columns
// of a matrix alternatively.
static void reverseAlternate(int[, ] arr)
{
int turn = 0;
while (turn < M && turn < N) {
if (turn % 2 == 0) {
int start = 0, end = N - 1, temp;
while (start < end) {
temp = arr[turn, start];
arr[turn, start] = arr[turn, end];
arr[turn, end] = temp;
start += 1;
end -= 1;
}
turn += 1;
}
if (turn % 2 == 1) {
int start = 0, end = M - 1, temp;
while (start < end) {
temp = arr[start, turn];
arr[start, turn] = arr[end, turn];
arr[end, turn] = temp;
start += 1;
end -= 1;
}
turn += 1;
}
}
}
// Driver code
public static void Main()
{
int[, ] matrix = { { 3, 4, 1, 8 },
{ 11, 23, 43, 21 },
{ 12, 17, 65, 91 },
{ 71, 56, 34, 24 } };
reverseAlternate(matrix);
showArray(matrix);
}
}
// This code is contributed by ukasp.
Javascript
输出
8 56 4 24
11 17 43 12
91 65 23 21
71 1 34 3
时间复杂度:O(M*N)
空间复杂度:O(1),不使用额外的额外空间。