package org.bluebridge.lx;
/*
*回形取数
问题描述
回形取数就是沿矩阵的边取数,若当前方向上无数可取或已经取过,则左转90度。一开始位于矩阵左上角,方向向下。
输入格式
输入第一行是两个不超过200的正整数m, n,表示矩阵的行和列。接下来m行每行n个整数,表示这个矩阵。
输出格式
输出只有一行,共mn个数,为输入矩阵回形取数得到的结果。数之间用一个空格分隔,行末不要有多余的空格。
样例输入
3 3
1 2 3
4 5 6
7 8 9
样例输出
1 4 7 8 9 6 3 2 5
样例输入
3 2
1 2
3 4
5 6
样例输出
1 3 5 6 4 2 */
import java.util.Scanner;
public class BASIC_25 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), m = sc.nextInt();
int[][] arr = new int[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
arr[i][j] = sc.nextInt();
sc.close();
int count = n * m, x = 0, y = 0, minx = 0, miny = 0;
while (count > 0) {
// 打印左边
while (x < n && count > 0) {
System.out.print(arr[x++][y] + " ");
count--;
}
x--;
y++;
n--;
// 打印下边
while (y < m && count > 0) {
System.out.print(arr[x][y++] + " ");
count--;
}
x--;
y--;
m--;
// 打印右边
while (x >= minx && count > 0) {
System.out.print(arr[x--][y] + " ");
count--;
}
x++;
y--;
minx++;
// 打印上边
while (y > miny && count > 0) {
System.out.print(arr[x][y--] + " ");
count--;
}
x++;
y++;
miny++;
}
}
}
回形取数
最新推荐文章于 2017-07-21 21:36:00 发布