原文:
给定一个大小为 n*n 的矩阵,按照以下模式打印该矩阵。
产量:1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 16 例:
input :matrix[2][2]= { {1, 2},
{3, 4} }
output : 1 2 3 4
input :matrix[3][3]= { {1, 2, 3},
{4, 5, 6},
{7, 8, 9} }
output : 1 2 4 3 5 7 6 8 9
下面是上述模式的 c 实现。
c
// cpp program to print matrix downward
#include
using namespace std;
void printmatrixdiagonallydown(vector > matrix,
int n)
{
// printing elements above and on
// second diagonal
for (int k = 0; k < n; k ) {
// traversing downwards starting
// from first row
int row = 0, col = k;
while (col >= 0) {
cout << matrix[row][col] << " ";
row , col--;
}
}
// printing elements below second
// diagonal
for (int j = 1; j < n; j ) {
// traversing downwards starting
// from last column
int col = n - 1, row = j;
while (row < n) {
cout << matrix[row][col] << " ";
row , col--;
}
}
}
int main()
{
vector > matrix{ { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };
int n = 3;
printmatrixdiagonallydown(matrix, n);
return 0;
}
java 语言(一种计算机语言,尤用于创建网站)
// java program to print
// matrix downward
class gfg{
static void printmatrixdiagonallydown(int[][] matrix,
int n)
{
// printing elements above and on
// second diagonal
for (int k = 0; k < n; k )
{
// traversing downwards
// starting from first row
int row = 0, col = k;
while (col >= 0)
{
system.out.print(matrix[row][col] " ");
row ;
col--;
}
}
// printing elements below
// second diagonal
for (int j = 1; j < n; j )
{
// traversing downwards starting
// from last column
int col = n - 1, row = j;
while (row < n)
{
system.out.print(matrix[row][col] " ");
row ;
col--;
}
}
}
// driver code
public static void main(string[] args)
{
int[][] matrix = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
int n = 3;
printmatrixdiagonallydown(matrix, n);
}
}
// this code is contributed by rajput-ji
python 3
# python 3 program to print matrix downward
def printmatrixdiagonallydown(matrix,n):
# printing elements above and on
# second diagonal
for k in range(n):
# traversing downwards starting
# from first row
row = 0
col = k
while (col >= 0):
print(matrix[row][col],end = " ")
row = 1
col -= 1
# printing elements below second
# diagonal
for j in range(1,n):
# traversing downwards starting
# from last column
col = n - 1
row = j
while (row < n):
print(matrix[row][col],end = " ")
row = 1
col -= 1
if __name__ == '__main__':
matrix = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]
n = 3
printmatrixdiagonallydown(matrix, n)
# this code is contributed by surendra_gangwar
c
// c# program to print
// matrix downward
using system;
class gfg{
static void printmatrixdiagonallydown(int[,] matrix,
int n)
{
// printing elements above and on
// second diagonal
for (int k = 0; k < n; k )
{
// traversing downwards
// starting from first row
int row = 0, col = k;
while (col >= 0)
{
console.write(matrix[row,col] " ");
row ;
col--;
}
}
// printing elements below
// second diagonal
for (int j = 1; j < n; j )
{
// traversing downwards starting
// from last column
int col = n - 1, row = j;
while (row < n)
{
console.write(matrix[row,col] " ");
row ;
col--;
}
}
}
// driver code
public static void main(string[] args)
{
int[,] matrix = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
int n = 3;
printmatrixdiagonallydown(matrix, n);
}
}
// this code is contributed by amit katiyar
java 描述语言
输出:
1 2 4 3 5 7 6 8 9
时间复杂度:o(n*n)
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处