2016-09-02 10:03
天津大学 前端工程师 呵呵哒2333:int n = in.nextInt();
//输出的矩阵 int arr[][] = new int[n][n]; int count = n * n; int num = 1; //位置索引 int x = 0; int y = 0; while (count > 0) { //向右走 do {
arr[x][y] = num++;
count--;
y++;
} while (y < n && arr[x][y] == 0); if (count == 0) break;
x++;
y--; //向下走 do {
arr[x][y] = num++;
count--;
x++;
} while (x < n && arr[x][y] == 0); if (count == 0) break;
y--;
x--; //向左走 do {
arr[x][y] = num++;
count--;
y--;
} while (y >= 0 && arr[x][y] == 0); if (count == 0) break;
y++;
x--; //像上走 do {
arr[x][y] = num++;
count--;
x--;
} while (x >= 0 && arr[x][y] == 0); if (count == 0) break;
x++;
y++;
}

0 点赞 评论 收藏
分享
创作者周榜
更多
关注他的用户也关注了: