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
热门推荐
10 异性朋友简短生日祝福语
11 爱情祝福语大全 简短实际
12 晚儿子生日祝福语简短
13 别人过大寿祝福语简短
14 喜事敬酒祝福语简短精辟
15 学生节祝福语简短大全
16 俄语祝福语贺卡文案简短
17 对女友的简短祝福语
18 药店祝福语简短10字