【模拟】旋转矩阵

#include<stdio.h>
int main(){
    int n;
    scanf("%d",&n);
    int a[n][n];
    int top=0,down=n-1,left=0,right=n-1,count=1;
    while(count<=n*n){
        for (int i=left;i<=right;i++){
            a[top][i]=count++;
        }
        top++;
        for (int i=top;i<=down;i++){
            a[i][right]=count++;
        }
        right--;
        for (int i=right;i>=left;i--){
            a[down][i]=count++;
        }
        down--;
        for (int i=down;i>=top;i--){
            a[i][left]=count++;
        }
        left++;
    }
    for (int i=0;i<n;i++){
        for (int j=0;j<n;j++){
            printf("%3d",a[i][j]);
        }
        printf("\n");
    }
    return 0;
}
© 版权声明
THE END
点赞10 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容