样例输入 复制5样例输出 复制 1 2 6 7 15 3 5 8 14 16 4 9 13 17 22 10 12 18 21 23 11 19 20 24 25c++代码
#include
using namespace std;
vector<vector
while (left <= right && top <= bottom) {
for (int i = left; i <= right; i++) {
matrix[top][i] = num++;
}
top++;
for (int i = top; i <= bottom; i++) {
matrix[i][right] = num++;
}
right--;
if (top <= bottom) {
for (int i = right; i >= left; i--) {
matrix[bottom][i] = num++;
}
bottom--;
}
if (left <= right) {
for (int i = bottom; i >= top; i--) {
matrix[i][left] = num++;
}
left++;
}
}
return matrix;
}
int main() { int n; cin >> n;
vector<vector<int>> matrix = generateMatrix(n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
return 0;
原文地址: http://www.cveoy.top/t/topic/iDbY 著作权归作者所有。请勿转载和采集!