- import javax.swing.JOptionPane;
- public class NeiLuoXuan01 {
- public static void main(String[] args) {
- String s = JOptionPane.showInputDialog("请输入行列数:");
- int count = Integer.parseInt(s);
- int max = count * count;
- int[][] data = new int[count][count];
- int x = -1, y = 0, cur = 0;
-
- while (cur < max) {
- for (; x + 1 < count && data[x + 1][y] == 0; data[y][++x] = ++cur);
- for (; y + 1 < count && data[x][y + 1] == 0; data[++y][x] = ++cur);
- for (; x - 1 >= 0 && data[y][x - 1] == 0; data[y][--x] = ++cur);
- for (; y - 1 >= 0 && data[y - 1][x] == 0; data[--y][x] = ++cur);
- }
-
- for (int i = 0; i < count; ++i) {
- for (int j = 0; j < data[i].length; ++j) {
- System.out.printf("%-3d ", data[i][j]);
- }
- System.out.println();
- }
- }
- }
复制代码
来,给你看个很屌的,不是我写的,我也看不懂,有啥问题也别问我呐 |