原文:
给定大小为n * m
的矩阵和点p(c, r)
。 从点p
开始以螺旋形式(顺时针)打印矩阵。
示例:
input : mat[][] = {{1 2 3},
{4 5 6},
{7 8 9}}
point p = (0, 2)
output : 3 6 5 2 9 8 7 4 1
the starting point is top left which
is 3.
这个问题主要是打印矩阵的扩展。
c
// c program to print a matrix in spiral
// form.
#include
using namespace std;
const int max = 100;
void printspiral(int mat[][max], int r, int c)
{
int i, a = 0, b = 2;
int low_row = (0 > a) ? 0 : a;
int low_column = (0 > b) ? 0 : b - 1;
int high_row = ((a 1) >= r) ? r - 1 : a 1;
int high_column = ((b 1) >= c) ? c - 1 : b 1;
while ((low_row > 0 - r && low_column > 0 - c)) {
for (i = low_column 1; i <= high_column &&
i < c && low_row >= 0; i)
cout << mat[low_row][i] << " ";
low_row -= 1;
for (i = low_row 2; i <= high_row && i < r &&
high_column < c; i)
cout << mat[i][high_column] << " ";
high_column = 1;
for (i = high_column - 2; i >= low_column &&
i >= 0 && high_row < r; --i)
cout << mat[high_row][i] << " ";
high_row = 1;
for (i = high_row - 2; i > low_row && i >= 0
&& low_column >= 0; --i)
cout << mat[i][low_column] << " ";
low_column -= 1;
}
cout << endl;
}
// driver code
int main()
{
int mat[][max] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
int r = 3, c = 3;
printspiral(mat, r, c);
}
java
// java program to print a
// matrix in spiral form
import java.io.*;
class gfg {
static void printspiral(int [][]mat, int r, int c)
{
int i, a = 0, b = 2;
int low_row = (0 > a) ? 0 : a;
int low_column = (0 > b) ? 0 : b - 1;
int high_row = ((a 1) >= r) ? r - 1 : a 1;
int high_column = ((b 1) >= c) ? c - 1 : b 1;
while ((low_row > 0 - r && low_column > 0 - c))
{
for (i = low_column 1; i <= high_column &&
i < c && low_row >= 0; i)
system.out.print (mat[low_row][i] " ");
low_row -= 1;
for (i = low_row 2; i <= high_row && i < r &&
high_column < c; i)
system.out.print(mat[i][high_column] " ");
high_column = 1;
for (i = high_column - 2; i >= low_column &&
i >= 0 && high_row < r; --i)
system.out.print(mat[high_row][i] " ");
high_row = 1;
for (i = high_row - 2; i > low_row && i >= 0
&& low_column >= 0; --i)
system.out.print(mat[i][low_column] " ");
low_column -= 1;
}
system.out.println();
}
// driver code
static public void main (string[] args)
{
int [][]mat = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
int r = 3, c = 3;
// function calling
printspiral(mat, r, c);
}
}
// this code is contributed by vt_m.
python 3
# python3 program to print a matrix
# in spiral form.
max = 100
def printspiral(mat, r, c):
a = 0
b = 2
low_row = 0 if (0 > a) else a
low_column = 0 if (0 > b) else b - 1
high_row = r-1 if ((a 1) >= r) else a 1
high_column = c-1 if ((b 1) >= c) else b 1
while ((low_row > 0 - r and low_column > 0 - c)):
i = low_column 1
while (i <= high_column and
i < c and low_row >= 0):
print( mat[low_row][i], end = " ")
i = 1
low_row -= 1
i = low_row 2
while (i <= high_row and
i < r and high_column < c):
print(mat[i][high_column], end = " ")
i = 1
high_column = 1
i = high_column - 2
while (i >= low_column and
i >= 0 and high_row < r):
print(mat[high_row][i], end = " ")
i -= 1
high_row = 1
i = high_row - 2
while (i > low_row and
i >= 0 and low_column >= 0):
print(mat[i][low_column], end = " ")
i -= 1
low_column -= 1
print()
# driver code
if __name__ == "__main__":
mat = [[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]]
r = 3
c = 3
printspiral(mat, r, c)
# this code is contributed by ita_c
c#
// c# program to print a
// matrix in spiral form
using system;
class gfg {
static void printspiral(int [,]mat, int r, int c)
{
int i, a = 0, b = 2;
int low_row = (0 > a) ? 0 : a;
int low_column = (0 > b) ? 0 : b - 1;
int high_row = ((a 1) >= r) ? r - 1 : a 1;
int high_column = ((b 1) >= c) ? c - 1 : b 1;
while ((low_row > 0 - r && low_column > 0 - c))
{
for (i = low_column 1; i <= high_column &&
i < c && low_row >= 0; i)
console.write (mat[low_row,i] " ");
low_row -= 1;
for (i = low_row 2; i <= high_row && i < r &&
high_column < c; i)
console.write(mat[i,high_column] " ");
high_column = 1;
for (i = high_column - 2; i >= low_column &&
i >= 0 && high_row < r; --i)
console.write(mat[high_row,i] " ");
high_row = 1;
for (i = high_row - 2; i > low_row && i >= 0
&& low_column >= 0; --i)
console.write(mat[i,low_column] " ");
low_column -= 1;
}
console.writeline();
}
// driver code
static public void main ()
{
int [,]mat = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
int r = 3, c = 3;
// function calling
printspiral(mat, r, c);
}
}
// this code is contributed by vt_m.
php
$a) ?
0 : $a;
$low_column = (0 > $b) ?
0 : $b - 1;
$high_row = (($a 1) >= $r) ?
$r - 1 : $a 1;
$high_column = (($b 1) >= $c) ?
$c - 1 : $b 1;
while (($low_row > 0 - $r &&
$low_column > 0 - $c))
{
for ($i = $low_column 1;
$i <= $high_column &&
$i < $c && $low_row >= 0; $i)
echo $mat[$low_row][$i], " ";
$low_row -= 1;
for ($i = $low_row 2;
$i <= $high_row && $i < $r &&
$high_column < $c; $i)
echo $mat[$i][$high_column] , " ";
$high_column = 1;
for ($i = $high_column - 2;
$i >= $low_column &&
$i >= 0 && $high_row < $r; --$i)
echo $mat[$high_row][$i] , " ";
$high_row = 1;
for ($i = $high_row - 2;
$i > $low_row && $i >= 0 &&
$low_column >= 0; --$i)
echo $mat[$i][$low_column] , " ";
$low_column -= 1;
}
echo "\n";
}
// driver code
$mat = array(array(1, 2, 3),
array(4, 5, 6),
array(7, 8, 9));
$r = 3; $c = 3;
printspiral($mat, $r, $c);
// this code is contributed by aj_36
?>
输出:
3 6 5 2 9 8 7 4 1
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处