Run ID:65776

提交时间:2024-02-10 02:35:22

#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; }