总而言之就是输出形如下图的数组:
笔试过程中想的是找规律填充,结果太复杂了,没能解决;
结束后改用直接的方法,倒是一刻钟搞定,唉;
c++代码如下:
#include<iostream> #include <vector> #include<functional> int main() { using namespace std; int N = 0; cin >> N; cout << endl << endl; int n = N - 1; vector<vector<int>> mat(N, vector<int>(N, 0)); auto op1 = [](int& i, int& j){j++;}; //→ auto op2 = [](int& i, int& j){i++;}; //↓ auto op3 = [](int& i, int& j){j--;}; //← auto op4 = [](int& i, int& j){i--;}; //↑ function<void (int&, int&)> op = op1; int cur = 0; int i = 0, j = 0; int st = 0; while (cur++ != N * N) { mat[i][j] = cur; op(i, j); if (i == st && j == n) op = op2; if (i == n && j == n) op = op3; if (i == n && j == st) op = op4; if (i == st && j == st) {op = op1; st++; n--; i = st; j = st;} } for (auto& l : mat) { for (auto i : l) cout << i << "\t"; cout << endl; } }