原文:

给定n * n大小的矩阵,任务是以对角线图案打印其元素。

input : mat[3][3] = {{1, 2, 3},
                     {4, 5, 6},
                     {7, 8, 9}}
output : 1 2 4 7 5 3 6 8 9.
explanation: start from 1 
then from upward to downward diagonally i.e. 2 and 4
then from downward to upward diagonally i.e 7, 5, 3 
then from up to down diagonally i.e  6, 8 
then down to up i.e. end at 9.
input :  mat[4][4] =  {{1,  2,  3,  10},
                      {4,  5,  6,  11},
                      {7,  8,  9,  12},
                      {13, 14, 15, 16}}
output:  1 2 4 7 5 3 10 6 8 13 14 9 11 12 15 16 .
explanation: start from 1 
then from upward to downward diagonally i.e. 2 and 4
then from downward to upward diagonally i.e 7, 5, 3 
then from upward to downward diagonally i.e. 10 6 8 13
then from downward to upward diagonally i.e 14 9 11
then from upward to downward diagonally i.e. 12 15
then end at 16

方法:从图中可以看出,每个元素要么斜向上打印,要么斜向下打印。 从索引(0, 0)开始,对角向上打印元素,然后更改方向,改变列,对角向下打印。 该循环一直持续到到达最后一个元素为止。

算法

  1. 创建变量i = 0, j = 0以存储行和列的当前索引。

  2. 运行从 0 到n * n的循环,其中n是矩阵的边。

  3. 使用标志isup来确定方向是向上还是向下。 最初将isup = true设置为向上方向。

  4. 如果isup = 1,则通过增加列索引和减少行索引来开始打印元素。

  5. 同样,如果isup = 0,则减少列索引并增加行索引。

  6. 移至下一个列或行(下一个开始的行和列)。

  7. 这样做直到遍历所有元素。

实现

c

// c   program to print matrix in diagonal order 
#include  
using namespace std; 
const int max = 100; 
void printmatrixdiagonal(int mat[max][max], int n) 
{ 
    // initialize indexes of element to be printed next 
    int i = 0, j = 0; 
    // direction is initially from down to up 
    bool isup = true; 
    // traverse the matrix till all elements get traversed 
    for (int k = 0; k < n * n;) { 
        // if isup = true then traverse from downward 
        // to upward 
        if (isup) { 
            for (; i >= 0 && j < n; j  , i--) { 
                cout << mat[i][j] << " "; 
                k  ; 
            } 
            // set i and j according to direction 
            if (i < 0 && j <= n - 1) 
                i = 0; 
            if (j == n) 
                i = i   2, j--; 
        } 
        // if isup = 0 then traverse up to down 
        else { 
            for (; j >= 0 && i < n; i  , j--) { 
                cout << mat[i][j] << " "; 
                k  ; 
            } 
            // set i and j according to direction 
            if (j < 0 && i <= n - 1) 
                j = 0; 
            if (i == n) 
                j = j   2, i--; 
        } 
        // revert the isup to change the direction 
        isup = !isup; 
    } 
} 
int main() 
{ 
    int mat[max][max] = { { 1, 2, 3 }, 
                          { 4, 5, 6 }, 
                          { 7, 8, 9 } }; 
    int n = 3; 
    printmatrixdiagonal(mat, n); 
    return 0; 
} 

java

// java program to print matrix in diagonal order 
class gfg { 
    static final int max = 100; 
  
    static void printmatrixdiagonal(int mat[][], int n) 
    { 
        // initialize indexes of element to be printed next 
        int i = 0, j = 0; 
  
        // direction is initially from down to up 
        boolean isup = true; 
  
        // traverse the matrix till all elements get traversed 
        for (int k = 0; k < n * n;) { 
            // if isup = true then traverse from downward 
            // to upward 
            if (isup) { 
                for (; i >= 0 && j < n; j  , i--) { 
                    system.out.print(mat[i][j]   " "); 
                    k  ; 
                } 
  
                // set i and j according to direction 
                if (i < 0 && j <= n - 1) 
                    i = 0; 
                if (j == n) { 
                    i = i   2; 
                    j--; 
                } 
            } 
  
            // if isup = 0 then traverse up to down 
            else { 
                for (; j >= 0 && i < n; i  , j--) { 
                    system.out.print(mat[i][j]   " "); 
                    k  ; 
                } 
  
                // set i and j according to direction 
                if (j < 0 && i <= n - 1) 
                    j = 0; 
                if (i == n) { 
                    j = j   2; 
                    i--; 
                } 
            } 
  
            // revert the isup to change the direction 
            isup = !isup; 
        } 
    } 
  
    // driver code 
    public static void main(string[] args) 
    { 
        int mat[][] = { { 1, 2, 3 }, 
                        { 4, 5, 6 }, 
                        { 7, 8, 9 } }; 
  
        int n = 3; 
        printmatrixdiagonal(mat, n); 
    } 
} 
// this code is contributed by anant agarwal.

python 3

# python 3 program to print matrix in diagonal order 
max = 100
  
def printmatrixdiagonal(mat, n): 
    # initialize indexes of element to be printed next 
    i = 0
    j = 0
    k = 0
    # direction is initially from down to up 
    isup = true
  
     # traverse the matrix till all elements get traversed 
    while k= 0 and j= 0 and i

c

// c# program to print matrix in diagonal order 
using system; 
class gfg { 
    static int max = 100; 
  
    static void printmatrixdiagonal(int[, ] mat, int n) 
    { 
        // initialize indexes of element to be printed next 
        int i = 0, j = 0; 
  
        // direction is initially from down to up 
        bool isup = true; 
  
        // traverse the matrix till all elements get traversed 
        for (int k = 0; k < n * n;) { 
            // if isup = true then traverse from downward 
            // to upward 
            if (isup) { 
                for (; i >= 0 && j < n; j  , i--) { 
                    console.write(mat[i, j]   " "); 
                    k  ; 
                } 
  
                // set i and j according to direction 
                if (i < 0 && j <= n - 1) 
                    i = 0; 
                if (j == n) { 
                    i = i   2; 
                    j--; 
                } 
            } 
  
            // if isup = 0 then traverse up to down 
            else { 
                for (; j >= 0 && i < n; i  , j--) { 
                    console.write(mat[i, j]   " "); 
                    k  ; 
                } 
  
                // set i and j according to direction 
                if (j < 0 && i <= n - 1) 
                    j = 0; 
                if (i == n) { 
                    j = j   2; 
                    i--; 
                } 
            } 
  
            // revert the isup to change the direction 
            isup = !isup; 
        } 
    } 
  
    // driver code 
    public static void main() 
    { 
        int[, ] mat = { { 1, 2, 3 }, 
                        { 4, 5, 6 }, 
                        { 7, 8, 9 } }; 
  
        int n = 3; 
        printmatrixdiagonal(mat, n); 
    } 
} 
// this code is contributed by vt_m.

php

= 0 && $j < $n;$j  , $i--) 
            { 
                echo $mat[$i][$j]." "; 
                $k  ; 
            } 
  
            // set i and j according  
            // to direction 
            if ($i < 0 && $j <= $n - 1) 
                $i = 0; 
            if ($j == $n) 
            { 
                $i = $i   2; 
                $j--; 
            } 
        } 
  
        // if isup = 0 then  
        // traverse up to down 
        else
        { 
            for ( ; $j >= 0 &&  
                 $i<$n ; $i  , $j--) 
            { 
                echo $mat[$i][$j]." "; 
                $k  ; 
            } 
  
            // set i and j according 
            // to direction 
            if ($j < 0 && $i <= $n - 1) 
                $j = 0; 
            if ($i == $n) 
            { 
                $j = $j   2; 
                $i--; 
            } 
        } 
  
        // revert the isup to 
        // change the direction 
        $isup = !$isup; 
    } 
} 
  
    // driver code 
    $mat= array(array(1, 2, 3), 
          array(4, 5, 6), 
          array(7, 8, 9)); 
  
    $n = 3; 
    printmatrixdiagonal($mat, $n); 
  
// this code is contributed by mits  
?>

输出:

1 2 4 7 5 3 6 8 9

复杂度分析:

  • 时间复杂度:o(n * n)

    要遍历矩阵o(n * n),需要时间复杂度。 空间复杂度:o(1)

    由于不需要额外的空间。

替代实现:这是与上述相同方法的另一种简单且紧凑的实现。

c

// c   program to print matrix in diagonal order 
#include  
using namespace std; 
  
int main() 
{ 
    // initialize matrix 
    int mat[][4] = { { 1, 2, 3, 4 }, 
                     { 5, 6, 7, 8 }, 
                     { 9, 10, 11, 12 }, 
                     { 13, 14, 15, 16 } }; 
    // n - size 
    // mode - switch to derive up/down traversal 
    // it - iterator count - increases until it 
    // reaches n and then decreases 
    int n = 4, mode = 0, it = 0, lower = 0; 
  
    // 2n will be the number of iterations 
    for (int t = 0; t < (2 * n - 1); t  ) { 
        int t1 = t; 
        if (t1 >= n) { 
            mode  ; 
            t1 = n - 1; 
            it--; 
            lower  ; 
        } 
        else { 
            lower = 0; 
            it  ; 
        } 
        for (int i = t1; i >= lower; i--) { 
            if ((t1   mode) % 2 == 0) { 
                cout << (mat[i][t1   lower - i]) << endl; 
            } 
            else { 
                cout << (mat[t1   lower - i][i]) << endl; 
            } 
        } 
    } 
    return 0; 
} 
  
// this code is contributed by princiraj1992

java

// java program to print matrix in diagonal order 
public class matrixdiag { 
  
    public static void main(string[] args) 
    { 
        // initialize matrix 
        int[][] mat = { { 1, 2, 3, 4 }, 
                        { 5, 6, 7, 8 }, 
                        { 9, 10, 11, 12 }, 
                        { 13, 14, 15, 16 } }; 
        // n - size 
        // mode - switch to derive up/down traversal 
        // it - iterator count - increases until it 
        // reaches n and then decreases 
        int n = 4, mode = 0, it = 0, lower = 0; 
  
        // 2n will be the number of iterations 
        for (int t = 0; t < (2 * n - 1); t  ) { 
            int t1 = t; 
            if (t1 >= n) { 
                mode  ; 
                t1 = n - 1; 
                it--; 
                lower  ; 
            } 
            else { 
                lower = 0; 
                it  ; 
            } 
            for (int i = t1; i >= lower; i--) { 
                if ((t1   mode) % 2 == 0) { 
                    system.out.println(mat[i][t1   lower - i]); 
                } 
                else { 
                    system.out.println(mat[t1   lower - i][i]); 
                } 
            } 
        } 
    } 
}

python3

# python3 program to prmatrix in diagonal order 
  
# driver code 
  
# initialize matrix 
mat = [[ 1, 2, 3, 4 ], 
       [ 5, 6, 7, 8 ], 
       [ 9, 10, 11, 12 ], 
       [ 13, 14, 15, 16 ]]; 
         
# n - size 
# mode - switch to derive up/down traversal 
# it - iterator count - increases until it 
# reaches n and then decreases 
n = 4
mode = 0
it = 0
lower = 0
  
# 2n will be the number of iterations 
for t in range(2 * n - 1): 
    t1 = t 
    if (t1 >= n): 
        mode  = 1
        t1 = n - 1
        it -= 1
        lower  = 1
    else: 
        lower = 0
        it  = 1
  
    for i in range(t1, lower - 1, -1): 
        if ((t1   mode) % 2 == 0): 
            print((mat[i][t1   lower - i])) 
        else: 
            print(mat[t1   lower - i][i]) 
  
# this code is contributed by princiraj1992

c

// c# program to print matrix in diagonal order 
using system; 
  
public class matrixdiag { 
    // driver code 
    public static void main(string[] args) 
    { 
        // initialize matrix 
        int[, ] mat = { { 1, 2, 3, 4 }, 
                        { 5, 6, 7, 8 }, 
                        { 9, 10, 11, 12 }, 
                        { 13, 14, 15, 16 } }; 
        // n - size 
        // mode - switch to derive up/down traversal 
        // it - iterator count - increases until it 
        // reaches n and then decreases 
        int n = 4, mode = 0, it = 0, lower = 0; 
  
        // 2n will be the number of iterations 
        for (int t = 0; t < (2 * n - 1); t  ) { 
            int t1 = t; 
            if (t1 >= n) { 
                mode  ; 
                t1 = n - 1; 
                it--; 
                lower  ; 
            } 
            else { 
                lower = 0; 
                it  ; 
            } 
            for (int i = t1; i >= lower; i--) { 
                if ((t1   mode) % 2 == 0) { 
                    console.writeline(mat[i, t1   lower - i]); 
                } 
                else { 
                    console.writeline(mat[t1   lower - i, i]); 
                } 
            } 
        } 
    } 
} 
  
// this code contributed by rajput-ji

输出:

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