原文:
给定一个n x n
矩阵。在给定的矩阵中,您必须以蛇形图案打印矩阵的元素。
示例:
input :mat[][] = { {10, 20, 30, 40},
{15, 25, 35, 45},
{27, 29, 37, 48},
{32, 33, 39, 50}};
output : 10 20 30 40 45 35 25 15 27 29
37 48 50 39 33 32
input :mat[][] = { {1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
output : 1 2 3 6 5 4 7 8 9
我们遍历所有行。 对于每一行,我们检查它是偶数还是奇数。 如果是偶数,我们从左到右打印,否则从右到左打印。
c
// c program to print matrix in snake order
#include
#define m 4
#define n 4
using namespace std;
void print(int mat[m][n])
{
// traverse through all rows
for (int i = 0; i < m; i ) {
// if current row is even, print from
// left to right
if (i % 2 == 0) {
for (int j = 0; j < n; j )
cout << mat[i][j] << " ";
// if current row is odd, print from
// right to left
} else {
for (int j = n - 1; j >= 0; j--)
cout << mat[i][j] << " ";
}
}
}
// driver code
int main()
{
int mat[][] = { { 10, 20, 30, 40 },
{ 15, 25, 35, 45 },
{ 27, 29, 37, 48 },
{ 32, 33, 39, 50 } };
print(mat);
return 0;
}
java
// java program to print matrix in snake order
import java.util.*;
class gfg
{
static void print(int [][] mat)
{
// traverse through all rows
for (int i = 0; i < mat.length; i )
{
// if current row is even, print from
// left to right
if (i % 2 == 0)
{
for (int j = 0; j < mat[0].length; j )
system.out.print(mat[i][j] " ");
// if current row is odd, print from
// right to left
}
else
{
for (int j = mat[0].length - 1; j >= 0; j--)
system.out.print(mat[i][j] " ");
}
}
}
// driver code
public static void main(string[] args)
{
int mat[][] = new int[][]
{
{ 10, 20, 30, 40 },
{ 15, 25, 35, 45 },
{ 27, 29, 37, 48 },
{ 32, 33, 39, 50 }
};
print(mat);
}
}
/* this code is contributed by mr. somesh awasthi */
python 3
# python 3 program to print
# matrix in snake order
m = 4
n = 4
def printf(mat):
global m, n
# traverse through all rows
for i in range(m):
# if current row is
# even, print from
# left to right
if i % 2 == 0:
for j in range(n):
print(str(mat[i][j]),
end = " ")
# if current row is
# odd, print from
# right to left
else:
for j in range(n - 1, -1, -1):
print(str(mat[i][j]),
end = " ")
# driver code
mat = [[ 10, 20, 30, 40 ],
[ 15, 25, 35, 45 ],
[ 27, 29, 37, 48 ],
[ 32, 33, 39, 50 ]]
printf(mat)
# this code is contributed
# by chitranayal
c#
// c# program to print
// matrix in snake order
using system;
class gfg
{
static void print(int [,]mat)
{
// traverse through all rows
for (int i = 0;
i < mat.getlength(0); i )
{
// if current row is
// even, print from
// left to right
if (i % 2 == 0)
{
for (int j = 0;
j < mat.getlength(1); j )
console.write(mat[i, j] " ");
// if current row is
// odd, print from
// right to left
}
else
{
for (int j = mat.getlength(1) - 1;
j >= 0; j--)
console.write(mat[i, j] " ");
}
}
}
// driver code
public static void main()
{
int [,]mat = {{ 10, 20, 30, 40 },
{ 15, 25, 35, 45 },
{ 27, 29, 37, 48 },
{ 32, 33, 39, 50 }};
print(mat);
}
}
// this code is contributed
// by chitranayal
php
= 0; $j--)
echo $mat[$i][$j] , " ";
}
}
}
// driver code
$mat = array(array(10, 20, 30, 40),
array(15, 25, 35, 45),
array(27, 29, 37, 48),
array(32, 33, 39, 50));
println($mat);
// this code is contributed by ajit
?>
输出:
10 20 30 40 45 35 25 15 27 29 37 48 50 39 33 32
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处