侧边栏
首页代码

旋转数组

#include<bits/stdc++.h>
using namespace std;
int a[25][25];
int main()
{
    int n;
    scanf("%d",&n);
    int x,y,tot = 1;
    memset(a,0,sizeof(a));
    a[x = 0][y = 0] = 1;
    while(tot < n * n)
    {
        while(a[x][y + 1] == 0 && y + 1 < n)
            a[x][++y] = ++tot;
        while(a[x + 1][y] == 0 && x + 1 < n)
            a[++x][y] = ++tot;
        while(a[x][y - 1] == 0 && y - 1 >= 0)
            a[x][--y] = ++tot;
        while(a[x - 1][y] == 0 && x - 1 >= 0)
            a[--x][y] = ++tot;
    }
    for(int i = 0;i < n;i++)
    {
        for(int j = 0;j < n;j++)
        {
            printf("%3d",a[i][j]);
        }
        printf("\n");
    }

return 0;
}
posted @ 2020-10-26 19:14  AlexStraightUp  阅读(53)  评论(1编辑  收藏  举报
页脚HTML代码