题目描述
写完蛇形矩阵后突然想到这个和蛇形矩阵有点类似。
样例
#include <iostream>
using namespace std;
int main(){
int n;
while (cin >> n,n != 0){
int left = 0,top = 0,right = n - 1,bottom = n - 1;
int k = 1;
int a[110][110];
while (left <= right && top <= bottom){
for (int i = left; i <= right; i++){
a[top][i] = k;
}
for (int i = top + 1; i <= bottom; i++) {
a[i][right] = k;
}
for (int i = right - 1; i >= left && top < bottom; i--){
a[bottom][i] = k;
}
for (int i = bottom - 1; i > top && left < right; i--){
a[i][left] = k;
}
top++;
bottom--;
left++;
right--;
k++;
}
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
cout << a[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
return 0;
}