原文:

给定一个mat【】【】,以波形打印。

输入: mat[][] = {{ 1,2,3,4} { 5,6,7,8} { 9,10,11,12} {13,14,15,16} {17,18,19,20}} 输出:1 5 9 13 17 18 14 10 6 2 3 11 15 19 20 16 12 8 4【t4

输入: mat[][] = {{1,9,4,10} { 3,6,90,11} { 2,30,85,72} { 6,31,99,15 } } t6】输出: 1 3 2 6 31 30 6 9 4 90 85 99 15 72 11 10

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

下面是上述方法的实现:

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

python 3

# 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

java 描述语言


output

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

时间复杂度:o(n2) 辅助空间: o(1)