| Run ID | 作者 | 问题 | 语言 | 测评结果 | Time | Memory | 代码长度 | 提交时间 |
|---|---|---|---|---|---|---|---|---|
| 141306 | 胡海峰老师 | 方阵填数 | C++ | Wrong Answer | 1 MS | 312 KB | 1209 | 2025-12-22 10:32:04 |
#include <iostream> using namespace std; int main() { int N; cin >> N; int matrix[101][101] = {0}; // 起始位置和数字 int row = 0; int col = N - 1; int num = 1; // 步长初始为N-1 int step = N - 1; // 方向:0=下,1=左,2=上,3=右 int dir = 0; // 填充第一个位置 matrix[row][col] = num++; while (step > 0) { // 每个方向填充step次 for (int i = 0; i < step; i++) { // 根据方向更新位置 if (dir == 0) row++; else if (dir == 1) col--; else if (dir == 2) row--; else if (dir == 3) col++; matrix[row][col] = num++; } // 改变方向 dir = (dir + 1) % 4; // 每完成两个方向,步长减1 if (dir % 2 == 0) { step--; } } // 输出结果 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; }
------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-----
0 0 0 0 0 0 0 0 0 1 27 28 29 30 31 32 33 34 35 2 26 55 56 57 58 59 60 61 36 3 25 54 75 76 77 78 79 62 37 4 24 53 74 87 88 89 80 63 38 5 23 52 73 86 91 90 81 64 39 6 22 51 72 85 84 83 82 65 40 7 21 50 71 70 69 68 67 66 41 8 20 49 48 47 46 45 44 43 42 9 19 18 17 16 15 14 13 12 11 10