题目传送门:P5731 【深基5.习6】蛇形方阵 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
题目描述
给出一个不大于 99 的正整数 n,输出 n×n 的蛇形方阵。
从左上角填上 11 开始,顺时针方向依次填入数字,如同样例所示。注意每个数字有都会占用 33 个字符,前面使用空格补齐。
输入格式
输入一个正整数 n,含义如题所述。
输出格式
输出符合题目要求的蛇形矩阵。
输入输出样例
4
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
看完整道题我无语了,我简直蒙了,我这个xxs根本不会做呀!
SO,上最麻烦的AC代码:
(其实我开了一点点科技……)(我对现代人的码风我无语了,连空格都懒得加😑)
#include <iostream>
using namespace std;
string b[2][2] = {
{" 1", " 2"},
{" 4", " 3"}
};
string c[3][3] = {
{" 1", " 2", " 3"},
{" 8", " 9", " 4"},
{" 7", " 6", " 5"}
};
string d[4][4] = {
{" 1", " 2", " 3", " 4"},
{" 12", " 13", " 14", " 5"},
{" 11", " 16", " 15", " 6"},
{" 10", " 9", " 8", " 7"},
};
string e[5][5] = {
{" 1", " 2", " 3", " 4", " 5"},
{" 16", " 17", " 18", " 19", " 6"},
{" 15", " 24", " 25", " 20", " 7"},
{" 14", " 23", " 22", " 21", " 8"},
{" 13", " 12", " 11", " 10", " 9"},
};
string f[6][6] = {
{" 1", " 2", " 3", " 4", " 5", " 6"},
{" 20", " 21", " 22", " 23", " 24", " 7"},
{" 19", " 32", " 33", " 34", " 25", " 8"},
{" 18", " 31", " 36", " 35", " 26", " 9"},
{" 17", " 30", " 29", " 28", " 27", " 10"},
{" 16", " 15", " 14", " 13", " 12", " 11"},
};
string g[7][7] = {
{" 1", " 2", " 3", " 4", " 5", " 6", " 7"},
{" 24", " 25", " 26", " 27", " 28", " 29", " 8"},
{" 23", " 40", " 41", " 42", " 43", " 30", " 9"},
{" 22", " 39", " 48", " 49", " 44", " 31", " 10"},
{" 21", " 38", " 47", " 46", " 45", " 32", " 11"},
{" 20", " 37", " 36", " 35", " 34", " 33", " 12"},
{" 19", " 18", " 17", " 16", " 15", " 14", " 13"},
};
string h[8][8] = {
{" 1", " 2", " 3", " 4", " 5", " 6", " 7", " 8"},
{" 28", " 29", " 30", " 31", " 32", " 33", " 34", " 9"},
{" 27", " 48", " 49", " 50", " 51", " 52", " 35", " 10"},
{" 26", " 47", " 60", " 61", " 62", " 53", " 36", " 11"},
{" 25", " 46", " 59", " 64", " 63", " 54", " 37", " 12"},
{" 24", " 45", " 58", " 57", " 56", " 55", " 38", " 13"},
{" 23", " 44", " 43", " 42", " 41", " 40", " 39", " 14"},
{" 22", " 21", " 20", " 19", " 18", " 17", " 16", " 15"},
};
string il[9][9] {
{" 1", " 2", " 3", " 4", " 5", " 6", " 7", " 8", " 9"},
{" 32", " 33", " 34", " 35", " 36", " 37", " 38", " 39", " 10"},
{" 31", " 56", " 57", " 58", " 59", " 60", " 61", " 40", " 11"},
{" 30", " 55", " 72", " 73", " 74", " 75", " 62", " 41", " 12"},
{" 29", " 54", " 71", " 80", " 81", " 76", " 63", " 42", " 13"},
{" 28", " 53", " 70", " 79", " 78", " 77", " 64", " 43", " 14"},
{" 27", " 52", " 69", " 68", " 67", " 66", " 65", " 44", " 15"},
{" 26", " 51", " 50", " 49", " 48", " 47", " 46", " 45", " 16"},
{" 25", " 24", " 23", " 22", " 21", " 20", " 19", " 18", " 17"},
};
int main() {
int n;
cin >> n;
if (n == 1) {
cout << " ";
cout << 1 << endl;
return 0;
}
if (n == 2) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << b[i][j];
}
cout << endl;
}
return 0;
}
if (n == 3) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << c[i][j];
}
cout << endl;
}
return 0;
}
if (n == 4) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << d[i][j];
}
cout << endl;
}
return 0;
}
if (n == 5) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << e[i][j];
}
cout << endl;
}
return 0;
}
if (n == 6) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << f[i][j];
}
cout << endl;
}
return 0;
}
if (n == 7) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << g[i][j];
}
cout << endl;
}
return 0;
}
if (n == 8) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << h[i][j];
}
cout << endl;
}
return 0;
}
if (n == 9) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << il[i][j];
}
cout << endl;
}
}
return 0;
}
这种方法可以用但是不建议使用(如果你不嫌手酸的话)
简单AC代码:
#include <iostream>
#include <iomanip>
using namespace std;
void printSnakeMatrix(int n) {
int matrix[100][100] = {0}; // 假设n不会超过100,可以根据实际情况调整数组大小
int num = 1, top = 0, bottom = n - 1, left = 0, right = n - 1;
while (num <= n * n) {
for (int i = left; i <= right; i++) matrix[top][i] = num++; // 填充上部
for (int i = top + 1; i <= bottom; i++) matrix[i][right] = num++; // 填充右部
for (int i = right - 1; i >= left; i--) matrix[bottom][i] = num++; // 填充下部
for (int i = bottom - 1; i > top; i--) matrix[i][left] = num++; // 填充左部
top++; bottom--; left++; right--;
}
// 打印蛇形方阵
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << setw(3) << matrix[i][j];
}
cout << endl;
}
}
int main() {
int n;
cin >> n;
printSnakeMatrix(n);
return 0;
}
看我这么辛苦给个赞再走吧……