第一种
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
int a[100][100] = {0};
scanf("%d", &n);//输入需要填充的数组的大小,a[n][n]
int cut = 1;
int x = 0;
int y = n-1;
a[x][y] = cut;
//printf ("a[0][n-1] = %d\n", cut);
while (cut < n*n) {
while (x+1 < n && a[x+1][y] == 0) {
//printf("(x+1) = %d y = %d cut+1 = %d\n", x+1, y, cut+1);
a[++x][y] = ++cut;
//printf ("竖加x = %d y = %d cut = %d\n", x, y, cut );
}
while (y-1 >= 0 && a[x][y-1] == 0) {
a[x][--y] = ++cut;
//printf ("横减x = %d y = %d cut = %d\n", x, y, cut );
}
while (x-1 >= 0 && a[x-1][y] == 0) {
a[--x][y] = ++cut;
//printf ("竖减x = %d y = %d cut = %d\n", x, y, cut );
}
while (y+1 < n && a[x][y+1] == 0) {
a[x][++y] = ++cut;
//printf ("横加x = %d y = %d cut = %d\n", x, y, cut );
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << a[i][j] << " ";
}
cout << endl;
}
return 0;
}
第二种
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
int a[100][100] = {0};
scanf("%d", &n);
int cut = 1;
int x = 0;
int y = 0;
a[x][y] = cut;
while (cut < n*n) {
while (x+1 < n && a[x+1][y] == 0)
a[++x][y] = ++cut;
while (y+1 < n && a[x][y+1] == 0)
a[x][++y] = ++cut;
while (y-1 >= 0 && a[x][y-1] == 0)
a[x][--y] = ++cut;
while (x-1 >= 0 && a[x-1][y] == 0)
a[--x][y] = ++cut;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << a[i][j] << " ";
}
cout << endl;
}
return 0;
}
看到这里的读者可以把剩下2种自己写出来(第3-4类情况,请不要看着代码写 自己闭卷写),注意1:4个小while判断条件没有增添1,只是探路的作用,括号外面增添数值,注意2:大while里面没有等于号
1 2 3
8 9 4
7 6 5
3 2 1
4 9 8
5 6 7
5个while的写法适合于所有大圈绕到圆心的蛇形填数
第5种
【问题描述】
如下图所示,小明用从1 开始的正整数“蛇形”填充无限大的矩阵。
容易看出矩阵第二行第二列中的数是5。请你计算矩阵中第20 行第20 列的数是多少?
【答案提交】
这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
int a[100][100] = {0};
//scanf("%d", &n);
int cnt = 1;
int x = 0;
int y = 0;
for (int i = 0; i < 50; i++) {
if (i%2 == 0) {
for (x = i, y = 0; x >= 0, y <= i; x--, y++)
a[x][y] = cnt++;
//cout << "偶数cut = " << cnt << endl;
}
else {
for (x = 0, y = i; x <= i, y >= 0; x++, y--)
a[x][y] = cnt++;
}
}
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
printf("%3d ", a[i][j]);
}
cout << endl;
}
}
控制住i,j 如果是偶数就斜向上递减,如果是奇数就斜向下递增;
第六种
1 6 7
2 5 8
3 4 9
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
int a[100][100] = {0};
scanf("%d", &n);
int cnt = 1;
int x ,y ;
for (int i = 0; i < n; i++) {
if (i%2 == 0) {
for (x = 0, y = i; x < n; x++)
a[x][y] = cnt++;
}
else {
for (x = n-1, y = i; x >= 0; x--)
a[x][y] = cnt++;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%3d ", a[i][j]);
}
cout << endl;
}
}
第七种
1 2 3
6 5 4
7 8 9
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
int a[100][100] = {0};
scanf("%d", &n);
int cnt = 1;
int x , y;
for (int i = 0; i < n; i++) {
if (i%2 == 0) {
for (x = i, y = 0; y < n; y++)
a[x][y] = cnt++;
}
else {
for (y = n-1, x = i; y >= 0; y--)
a[x][y] = cnt++;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%3d ", a[i][j]);
}
cout << endl;
}
}