Run ID | 作者 | 问题 | 语言 | 测评结果 | Time | Memory | 代码长度 | 提交时间 |
---|---|---|---|---|---|---|---|---|
5865 | 李伟和 | 二维数组输出(3) | C++ | Accepted | 5 MS | 256 KB | 1176 | 2024-12-20 14:52:10 |
#include <iostream> using namespace std; int main() { int n; cin >> n; if (n <= 0) { cout << "输入无效,N必须大于0。" << endl; return 1; } int a[10][10]; int value = 1; int top = 0, bottom = n - 1, left = 0, right = n - 1; while (top <= bottom && left <= right) { for (int i = left; i <= right; ++i) { a[top][i] = value++; } top++; for (int i = top; i <= bottom; ++i) { a[i][right] = value++; } right--; if (top <= bottom) { for (int i = right; i >= left; --i) { a[bottom][i] = value++; } bottom--; } if (left <= right) { for (int i = bottom; i >= top; --i) { a[i][left] = value++; } left++; } } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cout << a[i][j]; if (j < n - 1) { cout << " "; } } cout << endl; } return 0; // 返回0表示程序正常结束 }