回型矩阵 C++
回型矩阵
https://ac.nowcoder.com/acm/problem/22230
#include <iostream> #include <cmath> #include <algorithm> #include <vector> using namespace std; int a[20][20]; int main(){ int n, cnt = 1; cin >> n; for(int i = 0; i < n / 2 + 1; ++i){ for(int j = i; j < n - i; ++j) a[i][j] = cnt++; for(int j = i + 1; j < n - i; ++j) a[j][n - i - 1] = cnt++; for(int j = n - i - 2; j >= i; --j) a[n - i - 1][j] = cnt++; for(int j = n - i - 2; j > i; --j) a[j][i] = cnt++; } for(int i = 0; i < n; ++i){ for(int j = 0; j < n; ++j){ cout << a[i][j] << ' '; } cout << endl; } return 0; }