保留几位小数笔记
#include <iostream>
using namespace std;
const int N = 20;
int arr[N][N];//0
int main() {
int n;
cin >> n; //1~
int x = 0;
int y = n;
arr[1][y] = 0;
int snake = 1; //爬的距离
while (snake <= n * n) {
//往下爬
while (arr[x + 1][y] == 0 && x + 1 <= n) { //可不可以往下爬
//arr[x][y]==0 等于没爬过
//x+1<=n 超过n行
x += 1;
arr[x][y] = snake;
snake += 1;
//if(snake > n * n) break;
}
//break;
//往左爬
while (arr[x][y - 1] == 0 && y - 1 >= 1) { //可不可以往下爬
//arr[x][y]==0 等于没爬过
//x+1<=n 超过n行
y -= 1;
arr[x][y] = snake;
snake += 1;
}
//往上爬
while (arr[x - 1][y] == 0 && x - 1 >= 1) { //可不可以往下爬
//arr[x][y]==0 等于没爬过
//x+1<=n 超过n行
x -= 1;
arr[x][y] = snake;
snake += 1;
}
//往右爬
while (arr[x][y+1] == 0 && y + 1 <= n) { //可不可以往下爬
//arr[x][y]==0 等于没爬过
//x+1<=n 超过n行
y += 1;
arr[x][y] = snake;
snake += 1;
}
}
for (int i = 1; i <= n; i++) {
//TODO
for (int j = 1; j <= n; j++) {
cout << arr[i][j] << " ";//
}
cout << endl;
}
return 0;
}