Run ID | 作者 | 问题 | 语言 | 测评结果 | Time | Memory | 代码长度 | 提交时间 |
---|---|---|---|---|---|---|---|---|
114581 | 彭士宝 | 蛇形矩阵 | C++ | Wrong Answer | 1 MS | 276 KB | 1218 | 2025-03-22 17:28:24 |
#include <iostream> #include <vector> #include <iomanip> // 用于设置输出格式 using namespace std; int main() { int n; cin >> n; // 输入矩阵的大小 vector<vector<int>> matrix(n, vector<int>(n)); // 创建 n×n 的矩阵 int num = 1; // 当前要填充的数字 int row = n - 1, col = 0; // 从左下角开始 // 填充矩阵 while (num <= n * n) { matrix[row][col] = num++; if ((row + col) % 2 == 0) { // 向上移动 if (col == n - 1) { row--; } else if (row == 0) { col++; } else { row--; col++; } } else { // 向下移动 if (row == n - 1) { col++; } else if (col == 0) { row++; } else { row++; col--; } } } // 输出矩阵 for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cout << setw(3) << matrix[i][j]; // 每个数字占3个字符宽度 } cout << endl; } return 0; }
------Input------
4
------Answer-----
1 8 9 16 2 7 10 15 3 6 11 14 4 5 12 13
------Your output-----
0 0 0 12 0 0 13 11 0 14 10 0 15 16 0 0