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

比较经典的趣味题,算法本身不难,但此题设定最坑爹的是格式,要求动态调整字符宽度;要求的两组数据之间用空行隔开,其实是叫你从第二组开始在开头放一个空行,不是在每组测试之后放空行,这就要用一个little trick.

 #include <iostream>

 using namespace std;
const int size = ;
int main()
{
int n;
bool space = false;
while(cin>>n){
if(space)
cout<<endl;
int a[size][size] = {};
int x=,y=;
int total=a[x][y]=;
int max = n*n;
while(total<max){
while(y<n- && !a[x][y+]) a[x][++y] = ++total;
while(x<n- && !a[x+][y]) a[++x][y] = ++total;
while(y->= && !a[x][y-]) a[x][--y] = ++total;
while(x->= && !a[x-][y]) a[--x][y] = ++total;
}
int width;
if(max<) width = ;
if(max>=&&max<) width = ;
if(max>=&&max<) width = ;
if(max>=&&max<) width = ;
for(int i=;i<n;i++){
for(int j=;j<n;j++)
printf("%*d",width,a[i][j]);
cout<<endl;
}
space = true;
}
return ;
}

值得注意的是,这个是模拟算法,题目改了的话时间复杂度会很恐怖,得用算的

HrbustOJ 1564 螺旋矩阵的更多相关文章

  1. PAT 1050. 螺旋矩阵(25)

    本题要求将给定的N个正整数按非递增的顺序,填入"螺旋矩阵".所谓"螺旋矩阵",是指从左上角第1个格子开始,按顺时针螺旋方向填充.要求矩阵的规模为m行n列,满足条 ...

  2. leetcode-Spiral Matrix II 螺旋矩阵2之python大法好,四行就搞定,你敢信?

    Spiral Matrix II 螺旋矩阵 Given an integer n, generate a square matrix filled with elements from 1 to n2 ...

  3. Java-基础编程(螺旋矩阵&乘法表)

    package cn.rick.study; import java.io.BufferedReader;import java.io.InputStreamReader;import java.ut ...

  4. NOIP 2014 普及组 T3 螺旋矩阵

    [题意] 已知:n,r,c(n<=30000) 条件:给定n行n列的螺旋矩阵(从矩阵的左上角(1,1)出发,初始时向右移动:如果前方是未曾经过的格子, 则继续前进,否则右转:重复上述操作直至经过 ...

  5. PAT-乙级-1050. 螺旋矩阵(25)

    1050. 螺旋矩阵(25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题要求将给定的N个正整数按非递增的 ...

  6. NOIP2014-普及组复赛-第三题-螺旋矩阵

    题目描述 Description 一个n行n列的螺旋矩阵可由如下方法生成: 从矩阵的左上角(第1行第1列)出发,初始时向右移动:如果前方是未曾经过的格子,则继续前进,否则右转:重复上述操作直至经过矩阵 ...

  7. LeetCode 59. Spiral Matrix II (螺旋矩阵之二)

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  8. LeetCode 54. Spiral Matrix(螺旋矩阵)

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  9. Code Kata:螺旋矩阵 javascript实现

    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  如图所示,就是一个5*5的螺旋矩阵 我的思路如下: 第一步:拆分 ...

随机推荐

  1. How 5 Natural Language Processing APIs Stack Up

    https://www.programmableweb.com/news/how-5-natural-language-processing-apis-stack/analysis/2014/07/2 ...

  2. Django升级1.9.6出现的中文本地化bug

    Error日志: Error opening file for reading: Permission denied ERROR Internal Server Error: / Traceback  ...

  3. java基本数据类型及其包装类

    1.String类 String s1 = "hello world"; String s2 = "hello world"; String s3 = s1 + ...

  4. C# 使用SmtpClient发送Email

    使用Winfrom写的报错信息发送邮件通知. 以下主要代码 /// <summary> /// 发送邮件核心代码 /// </summary> /// <param na ...

  5. oracle中通过sql查询sde中图形面积

    select st_area(shape) from XAG2011430200000M_DLTB t where objectid=330

  6. C#备份及还原数据库的实现

    使用前要导入SQLDMO.dll 下载地址:http://down.51cto.com/data/853937 1.在用户的配置时,我们需要列出当前局域网内所有的数据库服务器,并且要列出指定服务器的所 ...

  7. 国内各大支付平台的API地址

    1丶目前国内最火的支付平台--蚂蚁金服开放平台(支付宝) https://open.alipay.com/platform/home.htm 2丶国内游戏帝国--腾讯(微信支付) https://pa ...

  8. Jenkins通过Publish over SSH插件实现远程部署

    Jenkins通过Publish over SSH插件实现远程部署 步凑一.配置ssh免秘钥登录 部署详情地址:http://www.cnblogs.com/Dev0ps/p/8259099.html ...

  9. Vlan 原理

    VLAN(Virtual LAN),翻译成中文是"虚拟局域网".LAN可以是由少数几台家用计算机构成的网络,也可以是数以百计的计算机构成的企业网络.VLAN所指的LAN特指使用路由 ...

  10. Oracle-04:DDL语言数据表的操作

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- DDL操作: --创建学生表 create table student( sno number(4) not  ...