forked from kexun/sort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SnakeNumber.java
76 lines (61 loc) · 1.24 KB
/
SnakeNumber.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.demo;
import java.util.Scanner;
/**
* 蛇数, 输入一个数字w, 成立一个w*w的矩阵,依次从1开始环绕输入如
* w = 3
*
* 1 2 3
* 8 9 4
* 7 6 5
* 最后输出 从上到下 输出每一行的数字 1 2 3 8 9 4 7 6 5
* @author kexun
*
*/
public class SnakeNumber {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
int w = in.nextInt();
int[][] a = SnakeNumber.sort(w);
for (int i=0; i<a.length; i++) {
for (int j=0; j<a[0].length; j++) {
System.out.print(a[i][j]);
}
System.out.println();
}
}
}
public static int[][] sort(int w) {
int[][] array = new int[w][w];
int a = 0;
int b = 0;
int index = 1;
while (index <= (w*w)) {
for (int j=0; j<w; j++) {
if (array[a][j] == 0) {
array[a][j] = index++;
b = j;
}
}
for (int i=0; i<w; i++) {
if (array[i][b] == 0) {
array[i][b] = index++;
a = i;
}
}
for (int j=b; j>=0; j--) {
if (array[a][j] == 0) {
array[a][j] = index++;
b = j;
}
}
for (int i=a; i>=0; i--) {
if (array[i][b] == 0) {
array[i][b] = index++;
a = i;
}
}
}
return array;
}
}