📜  以波形形式打印矩阵

📅  最后修改于: 2022-05-13 01:57:22.298000             🧑  作者: Mango

以波形形式打印矩阵

给定一个矩阵mat[][] ,以波形形式打印它。

方法:这个问题是基于实现的,并且具有与本文中讨论的类似的方法。要获得给定矩阵所需的波形,首先,向下打印矩阵第一列的元素,然后向上打印第二列的元素,然后打印第三列的元素向下的方向等等。

下面是上述方法的实现:

C++
// C++ program for above approach
#include 
using namespace std;
 
#define R 5
#define C 4
 
// Function to print wave
// Form for a given matrix
void WavePrint(int m, int n, int arr[R][C])
{
    // Loop to traverse matrix
    for (int j = 0; j < n; j++) {
 
        // If the current column
        // is even indexed, print
        // it in forward order
        if (j % 2 == 0) {
            for (int i = 0; i < m; i++) {
                cout << arr[i][j] << " ";
            }
        }
 
        // If the current column
        // is odd indexed, print
        // it in reverse order
        else {
            for (int i = m - 1; i >= 0; i--) {
                cout << arr[i][j] << " ";
            }
        }
    }
}
 
// Driver Code
int main()
{
    int arr[R][C] = { { 1, 2, 3, 4 },
                      { 5, 6, 7, 8 },
                      { 9, 10, 11, 12 },
                      { 13, 14, 15, 16 },
                      { 17, 18, 19, 20 } };
 
    WavePrint(R, C, arr);
 
    return 0;
}


C
// C Program for above approach
#include 
 
#define R 5
#define C 4
 
// Function to print wave
// Form for a given matrix
void WavePrint(int m, int n, int arr[R][C])
{
    // Loop to traverse matrix
    for (int j = 0; j < n; j++) {
 
        // If the current column
        // is even indexed, print
        // it in forward order
        if (j % 2 == 0) {
            for (int i = 0; i < m; i++) {
                printf("%d ", arr[i][j]);
            }
        }
 
        // If the current column
        // is odd indexed, print
        // it in reverse order
        else {
            for (int i = m - 1; i >= 0; i--) {
                printf("%d ", arr[i][j]);
            }
        }
    }
}
 
// Driver Code
int main()
{
    int arr[R][C] = { { 1, 2, 3, 4 },
                      { 5, 6, 7, 8 },
                      { 9, 10, 11, 12 },
                      { 13, 14, 15, 16 },
                      { 13, 14, 15, 16 } };
    WavePrint(R, C, arr);
 
    return 0;
}


Java
// Java program for above approach
import java.io.*;
class GFG {
 
  public static int R = 5;
  public static int C = 4;
 
  // Function to print wave
  // Form for a given matrix
  public static void WavePrint(int m, int n, int[][] arr)
  {
     
    // Loop to traverse matrix
    for (int j = 0; j < n; j++) {
 
      // If the current column
      // is even indexed, print
      // it in forward order
      if (j % 2 == 0) {
        for (int i = 0; i < m; i++) {
          System.out.print(arr[i][j] + " ");
        }
      }
 
      // If the current column
      // is odd indexed, print
      // it in reverse order
      else {
        for (int i = m - 1; i >= 0; i--) {
          System.out.print(arr[i][j] + " ");
        }
      }
    }
  }
 
  // Driver Code
  public static void main (String[] args)
  {
    int[][] arr = { { 1, 2, 3, 4 },
                   { 5, 6, 7, 8 },
                   { 9, 10, 11, 12 },
                   { 13, 14, 15, 16 },
                   { 17, 18, 19, 20 } };
 
    WavePrint(R, C, arr);
  }
}
 
// This code is contributed by Shubham Singh


Python3
# Python code for the above approach
R = 5
C = 4
 
# Function to print wave
# Form for a given matrix
def WavePrint(m, n, arr):
 
    # Loop to traverse matrix
    for j in range(n):
 
        # If the current column
        # is even indexed, print
        # it in forward order
        if (j % 2 == 0):
            for i in range(m):
                print(arr[i][j], end= " ")
 
        # If the current column
        # is odd indexed, print
        # it in reverse order
        else:
            for i in range(m - 1, -1, -1):
                print(arr[i][j], end= " ")
 
# Driver Code
arr = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16], [17, 18, 19, 20]]
 
WavePrint(R, C, arr)
 
# This code is contributed by gfgking


C#
// C# program for above approach
using System;
 
class GFG{
 
public static int R = 5;
public static int C = 4;
 
// Function to print wave
// Form for a given matrix
public static void WavePrint(int m, int n, int[,] arr)
{
 
    // Loop to traverse matrix
    for(int j = 0; j < n; j++)
    {
     
        // If the current column
        // is even indexed, print
        // it in forward order
        if (j % 2 == 0)
        {
            for(int i = 0; i < m; i++)
            {
                Console.Write(arr[i, j] + " ");
            }
        }
     
        // If the current column
        // is odd indexed, print
        // it in reverse order
        else
        {
            for(int i = m - 1; i >= 0; i--)
            {
                Console.Write(arr[i, j] + " ");
            }
        }
    }
}
 
// Driver Code
public static void Main ()
{
    int[,] arr = { { 1, 2, 3, 4 },
                   { 5, 6, 7, 8 },
                   { 9, 10, 11, 12 },
                   { 13, 14, 15, 16 },
                   { 17, 18, 19, 20 } };
     
    WavePrint(R, C, arr);
}
}
 
// This code is contributed by gfgking


Javascript


输出
1 5 9 13 17 18 14 10 6 2 3 7 11 15 19 20 16 12 8 4 

时间复杂度: O(N 2 )
辅助空间: O(1)