Run ID 作者 问题 语言 测评结果 Time Memory 代码长度 提交时间
119009 胡海峰老师 方阵填数 C++ Wrong Answer 1 MS 276 KB 1442 2025-05-11 18:29:13

Tests(0/3):


Code:

#include <iostream> #include <vector> using namespace std; void fillMatrix(vector<vector<int>>& matrix, int n) { int num = n * n; int top = 0, bottom = n - 1, left = 0, right = n - 1; while (top <= bottom && left <= right) { // 填充最右列,从上到下 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++; } // 填充最上行,从左到右 if (top <= bottom) { for (int i = left; i <= right; ++i) { matrix[top][i] = num--; } top++; } } } int main() { int n; cin >> n; vector<vector<int>> matrix(n, vector<int>(n, 0)); fillMatrix(matrix, n); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cout << matrix[i][j]; if (j < n - 1) cout << " "; } cout << endl; } return 0; }


Run Info:

------Input------
10
------Answer-----
28 29 30 31 32 33 34 35 36 1 27 58 59 60 61 62 63 64 37 2 26 57 80 81 82 83 84 65 38 3 25 56 79 94 95 96 85 66 39 4 24 55 78 93 100 97 86 67 40 5 23 54 77 92 99 98 87 68 41 6 22 53 76 91 90 89 88 69 42 7 21 52 75 74 73 72 71 70 43 8 20 51 50 49 48 47 46 45 44 9 19 18 17 16 15 14 13 12 11 10
------Your output-----
73 72 71 70 69 68 67 66 65 100 74 43 42 41 40 39 38 37 64 99 75 44 21 20 19 18 17 36 63 98 76 45 22 7 6 5 16 35 62 97 77 46 23 8 1 4 15 34 61 96 78 47 24 9 2 3 14 33 60 95 79 48 25 10 11 12 13 32 59 94 80 49 26 27 28 29 30 31 58 93 81 50 51 52 53 54 55 56 57 92 82 83 84 85 86 87 88 89 90 91