Java程序以螺旋形式打印给定的矩阵。
以下是一个Java程序,用于打印给定矩阵的螺旋形式。
示例
public class PrintMatrixInSpiralForm {public static void main(String args[]){
int a[][]={{1,2,3},{4,5,6},{7,8,9}};
int w = 0;
int x = a.length-1;
int y = 0;
int z = a[0].length-1;
while(w <= x && y <= z){
for (int i = w; i <= z; i++) {
System.out.print(a[w][i] + " ");
}
for (int i = w+1; i <= x; i++) {
System.out.print(a[i][z] + " ");
}
if(w+1 <= x){
for (int i = z-1; i >= y; i--) {
System.out.print(a[x][i] + " ");
}
}
if(y+1 <= z){
for (int i = x-1; i > w; i--) {
System.out.print(a[i][y] + " ");
}
}
w++;
x--;
y++;
z--;
}
}
}
输出结果
1 2 3 6 9 8 7 4 5
以上是 Java程序以螺旋形式打印给定的矩阵。 的全部内容, 来源链接: utcz.com/z/322472.html