题解 | 回型矩阵

回型矩阵

https://www.nowcoder.com/practice/36d5dfddc22c4f5b88a5b2a9de7db343

#include <stdio.h>
int main() {
    int n = 0;
    scanf("%d", &n);
    int a[n][n];
    int top = 0;
    int bottom = n - 1;
    int left = 0;
    int right = n - 1;
    int i = 0;
    int num = 1;

    while (top <= bottom)
    {
        //填充丄侧
        for (i=left; i<=right; ++i)
        {
            a[top][i] = num;
            ++num;        
        }
        ++top;

        //填充右侧
        for (i=top; i<=bottom; ++i)
        {
            a[i][right] = num;
            ++num;
        }
        --right;

        //填充下侧
        for (i=right; i>=left; --i)
        {
            a[bottom][i] = num;
            ++num;
        }
        --bottom;

        //填充左侧
        for (i=bottom; i>=top; --i)
        {
            a[i][left] = num;
            ++num;
        }
        ++left;
    }

    for (i=0; i<n; ++i)
    {
        for (int j=0; j<n; ++j)
        {
            printf("%d ", a[i][j]);
        }
        printf("\n");
    }

    return 0;
}

全部评论

相关推荐

码农索隆:有点耳熟,你们是我教过最差的一届
点赞 评论 收藏
分享
程序员小白条:你是沟通了900个,不是投了900份简历,你能投900份,意味着对面都要回复你900次,你早就找到实习了,没亮点就是这样的,别局限地区,时间投的也要早,现在都要7月了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务