Run ID 作者 问题 语言 测评结果 Time Memory 代码长度 提交时间
2245 孙浚轩 方阵填数 C++ Wrong Answer 0 MS 276 KB 1136 2023-12-23 00:29:48

Tests(0/3):


Code:

#include <iostream> #include <vector> void generateMatrix(int N) { std::vector<std::vector<int> > matrix(N, std::vector<int>(N, 0)); int num = 1; int row = 0, col = 0; int layer; if (N % 2 == 0) { layer = N / 2; } else { layer = (N + 1) / 2; } for (int i = 0; i < layer; ++i) { for (col = i; col < N - i; ++col) { matrix[i][col] = num++; } for (row = i + 1; row < N - i; ++row) { matrix[row][N - i - 1] = num++; } for (col = N - i - 2; col >= i; --col) { matrix[N - i - 1][col] = num++; } for (row = N - i - 2; row > i; --row) { matrix[row][i] = num++; } } for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { std::cout << matrix[i][j]; if (j < N - 1) { std::cout << " "; } } if (i < N - 1) { std::cout << std::endl; } } } int main() { int N; std::cin >> N; generateMatrix(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