62,614
社区成员
发帖
与我相关
我的任务
分享
import java.text.*;
public class Snake {
private static DecimalFormat df2 = new DecimalFormat("00");
public static void main(String[] args) {
int M = 4;
int N = 6;
boolean firstDown = true;
for(int n=0; n<N; n++){
for(int m=0; m<M; m++){
System.out.print(df2.format(getSnake(m, n, M, N, firstDown)));
System.out.print(" ");
}
System.out.println();
}
}
private static int getSnake(final int m, final int n, final int M, final int N, boolean firstDown){
final int MM = M<N?M:N;
final int NN = M<N?N:M;
final int K = M+N;
final int k = m+n;
int delta;
int max;
if(k<MM){
delta = firstDown?(k%2==0?(n+1):(m+1)):(k%2==0?(m+1):(n+1));
max = k*(k+1)/2;
}else
if(k<NN){
max = MM*(MM-1)/2+(k-MM+1)*MM;
delta = firstDown?(k%2==0?(N>M?M-m:n+1):(N>M?m+1:N-n)):(k%2==0?(N>M?m+1:N-n):(N>M?M-m:n+1));
}else{
max = N*M-(K-k)*(K-k-1)/2;
delta = firstDown?(k%2==0?(M-m):(N-n)):(k%2==0?(N-n):(M-m));
}
return max + delta;
}
}
01 03 04 10
02 05 09 11
06 08 12 18
07 13 17 19
14 16 20 23
15 21 22 24
01 02 06 07
03 05 08 14
04 09 13 15
10 12 16 21
11 17 20 22
18 19 23 24
01 03 04 10 11 18
02 05 09 12 17 19
06 08 13 16 20 23
07 14 15 21 22 24
01 02 06 07 14 15
03 05 08 13 16 21
04 09 12 17 20 22
10 11 18 19 23 24
01 02 06 07 15 16
03 05 08 14 17 26
04 09 13 18 25 27
10 12 19 24 28 33
11 20 23 29 32 34
21 22 30 31 35 36
package pitaya;
/**
*
* @author lv9
*
*/
public class NumberWeaving {
public static void main(String[] args) {
show(getArray(9));
}
public static void show(int[][] array) {
for (int i = 0; i < array.length; i++) {
for (int y = 0; y < array[i].length; y++) {
if (array[i][y] < 10) {
System.out.print("0");
}
System.out.print(array[i][y]);
System.out.print(" ");
}
System.out.println();
System.out.println();
}
}
public static int[][] getArray(final int dimension) {
int[][] array = new int[dimension][dimension];
int currentNumber = 1;
for (int x = 0; x < dimension; x++) {
for (int y = 0; y < x + 1; y++) {
if (x % 2 == 0) {
array[x - y][y] = currentNumber++;
} else {
array[y][x - y] = currentNumber++;
}
}
}
for (int x = dimension - 1; x > 0; x--) {
for (int y = 1; y <= x; y++) {
if (x % 2 == 0) {
array[y + dimension - x - 1][dimension - y] = currentNumber++;
} else {
array[dimension - y][y + dimension - x - 1] = currentNumber++;
}
}
}
return array;
}
}