hrbustoj 1564 螺旋矩阵 【模拟】

螺旋矩阵

 
Time Limit: 1000 MSMemory Limit: 10240 K
Total Submit: 396(102 users)Total Accepted: 110(94 users)Rating:Special Judge: No
Description

对于给定的一个数n,要你打印n*n的螺旋矩阵。

比如n=3时,输出:

 1 2 3
 8 9 4
 7 6 5

Input
多组测试数据,每个测试数据包含一个整数n(1<=n<=32)
Output

对于每组测试数据,输出一个n*n的螺旋矩阵,定义在题目描述里。

在一组测试数据中,每个数占的字符宽度是该组数据中最大的数位数加1,比如3*3的螺旋矩阵,最大值是9,那么每个数就要占2个字符宽度。

两组测试数据之间用一个空行隔开。

Sample Input
1
2
3
Sample Output
 1

 1 2
 4 3

 1 2 3
 8 9 4
 7 6 5
这题其实简单模拟就行。。。。。

主要判断下n是偶数还是奇数。。然后弄几个for循环,找到合适的时候跳出即可。



这题最大的收获是知道了怎么控制输出的长度:::

 printf("%*d",sum2+1,re[i][j]);///sum2是输出的长度


注意啊;;题目说两组测试数据之间用一个空行隔开。千万别忘了。

上代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int re[100][100];
int ls,sum,cs;
int h,l;
int zs;
int main()
{
    int n;
    zs=0;

    while(~scanf("%d",&n))
    {
        if(zs!=0)
        {
            printf("\n");
        }
        zs++;

        memset(re,0,sizeof(re));
        
        if(n==1)///单独判断下1
        {
            printf("%2d\n",1);
            continue;
        }



        sum=n*n;///最大的数是n*n
        int sum2=0;
        int ls2=sum;
        while(ls2)
        {
            ls2/=10;
            sum2++;///计算位数
        }


        ls=1;
        if(n%2==0)
        {
            cs=n/2;
        }
        else
        {
            cs=(n+1)/2-1;
        }

        h=1;
        l=n;

        for(int s=0; s<cs; s++)///控制层数
        {

            for(int i=h; i<=l; i++)///四个循环对应每一层的四个边
            {

                re[h][i]=ls;
                ls++;
            }
            for(int i=h+1; i<=l; i++)
            {
                re[i][l]=ls;

                ls++;
            }
            for(int i=l-1; i>=h; i--)
            {

                re[l][i]=ls;
                ls++;
            }
            for(int i=l-1; i>=h+1; i--)
            {
                re[i][h]=ls;
                ls++;
            }

            l--;///进入下一层循环
            h++;

            if(n%2!=0)
            {
                if(ls==sum)///奇数时跳出时机
                {
                    re[cs+1][cs+1]=sum;
                    break;
                }
            }
            else
            {
                if(ls==sum+1)///偶数时跳出时机
                {

                    break;
                }
            }

        }


        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                printf("%*d",sum2+1,re[i][j]);///sum2是输出的长度
            }
            printf("\n");

        }

    }
    return 0;
}

//1  2  3  4
//12 13 14 5
//11 16 15 6
//10 9  8  7
//
//1  2   3   4   5
//16 17  18  19  6
//15 24  25  20  7
//14 23  22  21  8
//13 12  11  10  9
// 1 2 3
// 8 9 4
// 7 6 5



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值