蛇形填数
这是一条经典的模拟题,按照题目意思按步编写代码即可!
难点:要能妥善处理好数组边界,选择合适的起始点
public class Main {
public static void main(String[] args) {
int[][] arr=new int[50][50];
int x=0;//当前行
int y=1;//当前列
int num=2;//开始自增的值
//模拟填数过程
while (arr[19][19]==0){
while (y>=0){
arr[x++][y--]=num++;
}
y++;
arr[x--][y++]=num++;
while (x>=0){
arr[x--][y++]=num++;
}
x++;
arr[x++][y--]=num++;
}
arr[0][0]=1;
for (int i=0;i<50;i++){
for (int j=0;j<50;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
System.out.println(arr[19][19]);
}
}
运行示意图:
本题答案:
761