Run ID 作者 问题 语言 测评结果 Time Memory 代码长度 提交时间
65776 zyqll 方阵填数 C++ Accepted 2 MS 272 KB 1045 2024-02-10 02:35:22

Tests(3/3):


Code:

#include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; int array[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { array[i][j] = -1; } } int I = 0; int J = n - 1; int count = 1; int stepI = 1; int stepJ = 0; while (1) { array[I][J] = count++; I += stepI; J += stepJ; if (array[I][J] != -1) { I -= stepI; J -= stepJ; if (stepI == 0) { stepI = stepJ; stepJ = 0; } else { stepJ = -stepI; stepI = 0; } I += stepI; J += stepJ; if (array[I][J] != -1) { break; } } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cout << array[i][j] << " "; } cout << endl; } return 0; }