• SDUT OJ 螺旋矩阵


    螺旋方阵

     

    Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

    题目描述

    n×n的螺旋方阵当n=5和n=3时分别是如下的形式

     
    请给出一个程序,对于任意的输入n(0<n<11),输出按照上面规律所获得的n×n的螺旋方阵。

    输入

    输入第一行为整数m(0<m<10),代表有m组输入;
    接下来是m行数据,每行输入一个n(0<n<11)。

    输出

    按照输入的次序,依次输出每一个n×n方阵(一个方阵的同一行数据之间以' '分隔)
    两个输出方阵之间输出一个空行。

    示例输入

    1
    4

    示例输出

    1   2   3   4
    12  13   14   5
    11  16   15   6
    10   9    8   7
     
    #include <iostream>
    #include <string.h>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	int t, n;
    	int cnt;
        int a[15][15] ;
        int i, j;
    	
    	cin>>t;
        int x=0, y=0;
    	while(t--)
    	{
    		memset(a, 0, sizeof(a));
    		
            cin>>n;
            a[0][0] = 1;
    		cnt=1;
    		x=0, y=0;
            while(cnt < n*n)
    		{
    			while(y+1<n && a[x][y+1]==0 )
    			{
    				a[x][++y]=++cnt;
    			}
    			while(x+1<n && a[x+1][y]==0 )
    			{
    				a[++x][y]= ++cnt;
    			}
    			while(y>0 && a[x][y-1]==0 )
    			{
    				a[x][--y] = ++cnt;
    			}
    			while(x>0 && a[x-1][y]==0 )
    			{
    				a[--x][y] = ++cnt;
    			}
    		}
    		for(i=0; i<n; i++)
    		{
    			for(j=0; j<n; j++)
    			{
    				if(j==n-1)
    					cout<<a[i][j]<<endl ;
    				else
    				cout<<a[i][j]<<'	';
    			}
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    让Sendmail和Dovecot使用AD进行用户认证
    在dhcpd.conf中配置静态路由
    IPhone4与Exchange 2010同步失败
    /*从文本中读取文件*/
    EM数据包按规则更新
    Crystal Report Show in Web With ParameterField
    /*读取xml数据*/
    上传文件
    新的网站
    【练习】哥德巴赫猜想验证程序
  • 原文地址:https://www.cnblogs.com/yspworld/p/3967715.html
Copyright © 2020-2023  润新知