原文:

给定一个矩阵,以反向波形打印它。

示例:

input :  1  2  3  4
         5  6  7  8
         9  10 11 12
         13 14 15 16
output : 4 8 12 16 15 11 7 3 2 6 10 14 13 9 5 1
input :  1  9  4  10
         3  6  90 11
         2  30 85 72
         6  31 99 15 
output : 10 11 72 15 99 85 90 4 9 6 30 31 6 2 3 1

方法:为了得到给定矩阵的逆波形,我们首先向下打印矩阵最后一列的元素,然后向上打印第二最后一列的元素,然后向下打印第三最后一列的元素,以此类推。例如 1,流程如下:

下面是打印矩阵逆波形的实现:

c

// c   implementation to print
// reverse wave form of matrix
#include
using namespace std;
#define r 4
#define c 4
// function to print reverse wave
// form for a given matrix
void waveprint(int m, int n, int arr[r][c])
{
    int i, j = n - 1, wave = 1;
    /* m     - ending row index
        n     - ending column index
        i, j     - iterator
        wave     - for direction
        wave = 1 - wave direction down
        wave = 0 - wave direction up   */
    while (j >= 0) {
        // check whether to go in
        // upward or downward
        if (wave == 1) {
            // print the element of the matrix
            // downward since the value of wave = 1
            for (i = 0; i < m; i  )
                cout << arr[i][j] << " "; 
            wave = 0;
            j--;
        }
        else {
            // print the elements of the
            // matrix upward since the value
            // of wave = 0
            for (i = m - 1; i >= 0; i--)
                cout << arr[i][j] << " ";
            wave = 1;
            j--;
        }
    }
}
// driver function
int main()
{
    int arr[r][c] = { { 1, 2, 3, 4 },
                      { 5, 6, 7, 8 },
                      { 9, 10, 11, 12 },
                      { 13, 14, 15, 16 } };                   
    waveprint(r, c, arr);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java implementation to print
// reverse wave form of matrix
import java.io.*;
class gfg
{
    static int r = 4;
    static int c = 4;
    // function to print reverse wave
    // form for a given matrix
    static void waveprint(int m, int n, int arr[][])
    {
        int i, j = n - 1, wave = 1;
        // m- ending row index
        //n - ending column index
        //i, j     - iterator
        //wave     - for direction
        //wave = 1 - wave direction down
        //wave = 0 - wave direction up */
        while (j >= 0)
        {
            // check whether to go in
            // upward or downward
            if (wave == 1)
            {
                // print the element of the matrix
                // downward since the value of wave = 1
                for (i = 0; i < m; i  )
                    system.out.print(arr[i][j]  " ");
                wave = 0;
                j--;
            }
            else {
                // print the elements of the
                // matrix upward since the value
                // of wave = 0
                for (i = m - 1; i >= 0; i--)
                    system.out.print( arr[i][j]   " ");
                wave = 1;
                j--;
            }
        }
    }
    // driver function
    public static void main (string[] args)
    {
        int arr[][] = { { 1, 2, 3, 4 },
                    { 5, 6, 7, 8 },
                    { 9, 10, 11, 12 },
                    { 13, 14, 15, 16 } };            
        waveprint(r, c, arr);  
    }
}
// this code is contributed by vt_m

python 3

# python3 implementation to print
# reverse wave form of matrix
r = 4
c = 4
# function to print reverse wave
# form for a given matrix
def waveprint(m, n, arr):
    j = n - 1
    wave = 1
    # m     - ending row index
    # n     - ending column index
    # i, j     - iterator
    # wave     - for direction
    # wave = 1 - wave direction down
    # wave = 0 - wave direction up
    while j >= 0:
        # check whether to go in
        # upward or downward
        if wave == 1:
            # print the element of the
            # matrix downward since the
                        # value of wave = 1
            for i in range(m):
                print(arr[i][j], end = " "),
            wave = 0
            j -= 1
        else:
            # print the elements of the
            # matrix upward since the
            # value of wave = 0
            for i in range(m - 1, -1, -1):
                print(arr[i][j], end = " "),
            wave = 1
            j -= 1
# driver code
arr = [ [ 1, 2, 3, 4 ],
        [ 5, 6, 7, 8 ],
        [ 9, 10, 11, 12 ],
        [ 13, 14, 15, 16 ] ]
waveprint(r, c, arr)
# this code is contributed by
# upendra singh bartwal

c

// c# implementation to print
// reverse wave form of matrix
using system;
class gfg {
    static int r = 4;
    static int c = 4;
    // function to print reverse wave
    // form for a given matrix
    static void waveprint(int m, int n, int [,]arr)
    {
        int i, j = n - 1, wave = 1;
        // m- ending row index
        // n - ending column index
        // i, j - iterator
        // wave - for direction
        // wave = 1 - wave direction down
        // wave = 0 - wave direction up */
        while (j >= 0)
        {
            // check whether to go in
            // upward or downward
            if (wave == 1) {
                // print the element of the
                // matrix downward since the
                // value of wave = 1
                for (i = 0; i < m; i  )
                    console.write(arr[i,j]   " ");
                wave = 0;
                j--;
            }
            else {
                // print the elements of the
                // matrix upward since the value
                // of wave = 0
                for (i = m - 1; i >= 0; i--)
                    console.write( arr[i,j]   " ");
                wave = 1;
                j--;
            }
        }
    }
    // driver function
    public static void main ()
    {
        int [,]arr = { { 1, 2, 3, 4 },
                       { 5, 6, 7, 8 },
                       { 9, 10, 11, 12 },
                       { 13, 14, 15, 16 } };
        waveprint(r, c, arr);
    }
}
// this code is contributed by vt_m.

服务器端编程语言(professional hypertext preprocessor 的缩写)

= 0)
    {
        // check whether to go in
        // upward or downward
        if ($wave == 1)
        {
            // print the element of the
            // matrix downward since the
            // value of wave = 1
            for ($i = 0; $i < $m; $i  )
                echo $arr[$i][$j] , " ";
            $wave = 0;
            $j--;
        }
        else
        {
            // print the elements of
            // the matrix upward since
            // the value of wave = 0
            for ($i = $m - 1; $i >= 0; $i--)
                echo $arr[$i][$j] , " ";
            $wave = 1;
            $j--;
        }
    }
}
// driver code
$arr = array(array(1, 2, 3, 4),
             array(5, 6, 7, 8),
             array(9, 10, 11, 12),
             array(13, 14, 15, 16));                
waveprint($r, $c, $arr);
// this code is contributed by ajit
?>

java 描述语言


输出:

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