/**
*
* n*n矩阵由外到内顺时针打印值
* @author Youjc
* @date Apr 2, 2018
*/
public class ShuZuTest {
public ShuZuTest() {
// TODO Auto-generated constructor stub
// int[][] temp = new int[][] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10,
// 11, 12 }, { 13, 14, 15, 16 } };
int[][] temp = new int[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
// int[][] temp = new int[][] { {1,2},{3,4}};
// int[][] temp = new int[][] { {1}};
int rows = temp.length;
int columns = temp[0].length + 1;
int flag = 0;
if (rows % 2 == 0) {
flag = rows / 2;
} else {
flag = (rows + 1) / 2;
}
for (int i = 0; i < flag; i++) {
columns--;
rows--;
for (int j = i; j < columns; j++) {
System.out.print(temp[i][j]);
}
// System.out.println();
for (int j = i + 1; j < rows + 1; j++) {
System.out.print(temp[j][columns - 1]);
}
// System.out.println();
for (int j = columns - 2; j >= i; j--) {
System.out.print(temp[rows][j]);
}
// System.out.println();
for (int j = rows - 1; j > i; j--) {
System.out.print(temp[j][i]);
}
// System.out.println();
}
}
public static void main(String[] args) {
new ShuZuTest();
}
}
输入一个矩阵,由外向里顺时针打印数字
最新推荐文章于 2022-04-03 13:00:21 发布