Run ID 作者 问题 语言 测评结果 Time Memory 代码长度 提交时间
114582 彭士宝 蛇形矩阵 C++ Wrong Answer 1 MS 272 KB 1237 2025-03-22 17:29:03

Tests(0/10):


Code:

#include <iostream> #include <vector> #include <iomanip> // 用于设置输出格式 using namespace std; int main() { int n; cin >> n; // 输入矩阵的大小 vector<vector<int>> matrix(n, vector<int>(n, 0)); // 创建 n×n 的矩阵,初始值为0 int num = 1; // 当前要填充的数字 int row = 0, 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; }


Run Info:

------Input------
4
------Answer-----
1 8 9 16 2 7 10 15 3 6 11 14 4 5 12 13
------Your output-----
1 2 6 7 3 5 8 13 4 9 12 14 10 11 15 16