- 问答
第一题哪错了
- 2024-1-9 20:25:12 @
#include <bits/stdc++.h>
using namespace std;
int res[10][10];
int main()
{
int n;
cin >>n;
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
for (int x = 0, y = 0, d = 0, k = 1; k <= n * n; k ++ )
{
res[x][y] = k;
int a = x + dx[d], b = y + dy[d];
if (a < 0 || a >= n || b < 0 || b >= n|| res[a][b])
{
d = (d + 1) % 4;
a = x + dx[d], b = y + dy[d];
}
x = a, y = b;
}
for (int i = 0; i < n; i ++ )
{
for (int j = 0; j < n; j ++ ) cout <<setw(3)<<res[i][j] << ' ';
cout << endl;
}
return 0;
}
0 comments
No comments so far...