古镇建网站公司,优化推广的页面对于优化点击率起非常大的作用,医院网站建设利法拉网络,机关网站建设需求文档题目描述
给出一个不大于 9 的正整数 n#xff0c;输出 nn 的蛇形方阵。
从左上角填上 1 开始#xff0c;顺时针方向依次填入数字#xff0c;如同样例所示。注意每个数字有都会占用 3 个字符#xff0c;前面使用空格补齐。
输入格式
输入一个正整数 nn#xff0c;含义…题目描述
给出一个不大于 9 的正整数 n输出 n×n 的蛇形方阵。
从左上角填上 1 开始顺时针方向依次填入数字如同样例所示。注意每个数字有都会占用 3 个字符前面使用空格补齐。
输入格式
输入一个正整数 nn含义如题所述。
输出格式
输出符合题目要求的蛇形矩阵。
输入输出样例
输入 #1
4
输出 #1 1 2 3 412 13 14 511 16 15 610 9 8 7
说明/提示
数据保证1≤n≤9。
解
#includeiostream
#includealgorithm
#includecstring
using namespace std;
const int N 10;
int a[N][N];
int main()
{int n, k 1, x 1, y 0; cin n;while (k n * n){while (y n !a[x][y 1])a[x][y] k;while (x n !a[x 1][y])a[x][y] k;while (y 1 !a[x][y - 1])a[x][--y] k;while (x 1 !a[x - 1][y])a[--x][y] k;}for (int i 1; i n; i){for (int j 1; j n; j)printf(%3d, a[i][j]);cout endl;}return 0;
}