Run ID 作者 问题 语言 测评结果 Time Memory 代码长度 提交时间
100761 汤奕硕 方阵填数 C++ Wrong Answer 1 MS 272 KB 1228 2024-12-12 21:04:19

Tests(0/3):


Code:

#include <iostream> #include <vector> using namespace std; void printSpiralMatrix(int N) { vector<vector<int>> matrix(N, vector<int>(N)); int num = 1; // 要填入的数字 int top = 0, bottom = N - 1, left = 0, right = N - 1; while (num <= N * N) { // 从左到右填充顶行 for (int i = left; i <= right && num <= N * N; ++i) { matrix[top][i] = num++; } top++; // 从上到下填充右列 for (int i = top; i <= bottom && num <= N * N; ++i) { matrix[i][right] = num++; } right--; // 从右到左填充底行 for (int i = right; i >= left && num <= N * N; --i) { matrix[bottom][i] = num++; } bottom--; // 从下到上填充左列 for (int i = bottom; i >= top && num <= N * N; --i) { matrix[i][left] = num++; } left++; } // 打印矩阵 for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { cout << matrix[i][j] << " "; } cout << endl; } } int main() { int N; cin >> N; printSpiralMatrix(N); 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-----
1 2 3 4 5 6 7 8 9 10 36 37 38 39 40 41 42 43 44 11 35 64 65 66 67 68 69 70 45 12 34 63 84 85 86 87 88 71 46 13 33 62 83 96 97 98 89 72 47 14 32 61 82 95 100 99 90 73 48 15 31 60 81 94 93 92 91 74 49 16 30 59 80 79 78 77 76 75 50 17 29 58 57 56 55 54 53 52 51 18 28 27 26 25 24 23 22 21 20 19