蛇形填数

简介: 蛇形填数
样例输入:
4
样例输出:
10  11  12  1
9   16  13  2
8   15  14  3
7    6   5  4


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    int a[50][50];
    int i,j,x,y,cot,n;
    scanf("%d",&n);
    memset(a,0,sizeof(a));
    cot=a[0][n-1]=1;
    y=n-1,x=0;
    cot=2;
    while(cot<=n*n)
    {
        while(x+1<n&&(!a[x+1][y]))
        {
            a[++x][y]=cot++;
           // printf("1\n");
        }
        while(y-1>=0&&(!a[x][y-1]))
        {
            a[x][--y]=cot++;
            //printf("2\n");
        }
        while(x-1>=0&&(!a[x-1][y]))
        {
            a[--x][y]=cot++;
            //printf("3\n");
        }
        while(y+1<n&&(!a[x][y+1]))
        {
            a[x][++y]=cot++;
            //printf("4\n");
        }
    }
    for(i=0; i<n; i++)
    {
        for(j=0; j<n; j++)
            printf("%3d",a[i][j]);
        printf("\n");
    }
    return 0;
}
目录
相关文章
|
人工智能
蛇形填数
#include int main() { int k,i,j;int num=1,n; int a[100][100]; scanf("%d",&n); // for(i=1;i
660 0
|
9月前
|
人工智能
蛇形填数(蓝桥杯)
蛇形填数(蓝桥杯)
HDU 1166 敌兵布阵(线段树单点更新,板子题)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 87684    Accepted Submission(s): 36912 Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了。
1174 0
HDU-1058,Humble Numbers(丑数打表)
HDU-1058,Humble Numbers(丑数打表)
PTA猴子选大王(约瑟夫环问题)
PTA猴子选大王(约瑟夫环问题)
145 1
|
机器学习/深度学习 算法
【刷穿 LeetCode】第 N 个泰波那契数 :「迭代」&「递归」&「矩阵快速幂」&「打表」
【刷穿 LeetCode】第 N 个泰波那契数 :「迭代」&「递归」&「矩阵快速幂」&「打表」

热门文章

最新文章