数组经典作业
蛇形填数1
在n*n方阵里填入1,2,3,…,n*n,要求填成蛇形。
例如n=4时方阵为:
#include <iostream>
using namespace std;
#define max 100
//下—>左—>上->右
int main() {
int m, n, x, y, c;
int a[max][max];
cout << "请输入行数:";
cin >> m ;
c = 1;
n=m;
x = 0;
y = m - 1;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = 0;
}
}
a[x][y] = c++;
while (c <= m * n) {
while (x + 1 < m && a[x + 1][y] == 0) {//向下
a[++x][y] = c++;
}
while (y - 1 >= 0 && a[x][y - 1] == 0) {//向左
a[x][--y] = c++;
}
while (x - 1 >= 0 &