蛇形方阵c++顺时针输出
时间: 2024-01-27 14:12:50 浏览: 73
c++蛇形矩阵
5星 · 资源好评率100%
以下是蛇形方阵C++顺时针输出的代码:
```c++
#include <iostream>
#include <iomanip>
#define ll long long
using namespace std;
ll s[1000][1000];
int main() {
int n, i = 1, j = 0, sum = 1;
cin >> n;
while (sum <= n * n) {
while (s[i][++j] == 0 && j <= n) {
s[i][j] = sum++;
}
j--;
while (s[++i][j] == 0 && i <= n) {
s[i][j] = sum++;
}
i--;
while (s[i][--j] == 0 && j >= 1) {
s[i][j] = sum++;
}
j++;
while (s[--i][j] == 0 && i >= 1) {
s[i][j] = sum++;
}
i++;
}
for (j = 1; j <= n; j++) {
for (i = 1; i <= n; i++) {
cout << setw(3) << setfill(' ') << s[i][j];
}
cout << endl;
}
return 0;
}
```
阅读全文