This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasing order. A spiral matrixis filled in from the first element at the upper-left corner, then move in a clockwise spiral. The matrix has m rows and ncolumns, where m and n satisfy the following: m*n must be equal to N; m>=n; and m-n is the minimum of all the possible values.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N. Then the next line contains N positive integers to be filled into the spiral matrix. All the numbers are no more than 104. The numbers in a line are separated by spaces.

Output Specification:

For each test case, output the resulting matrix in m lines, each contains n numbers. There must be exactly 1 space between two adjacent numbers, and no extra space at the end of each line.

Sample Input:

12
37 76 20 98 76 42 53 95 60 81 58 93

Sample Output:

98 95 93
42 37 81
53 20 76
58 60 76
 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
int G[][];
int N, num[], index = ;
bool cmp(int a, int b){
return a > b;
}
int main(){
scanf("%d", &N);
for(int i = ; i < N; i++)
scanf("%d", &num[i]);
int sqr = sqrt(N * 1.0);
int m, n;
for(n = sqr; N % n != ; n--);
m = N / n;
sort(num, num + N, cmp);
int k = , j = , times = ;
while(index < N){
if(N - index == ){
G[k][j] = num[index];
break;
}
while(j < n - - times && index < N){
G[k][j++] = num[index++];
}
while(k < m - - times && index < N){
G[k++][j] = num[index++];
}
while(j > times && index < N){
G[k][j--] = num[index++];
}
while(k > times && index < N){
G[k--][j] = num[index++];
}
k++;
j++;
times++;
}
for(int i = ; i < m; i++){
for(int j = ; j < n; j++){
if(j == n - )
printf("%d", G[i][j]);
else
printf("%d ", G[i][j]);
}
printf("\n");
}
cin >> N;
return ;
}

总结:

1、采用旋转填充二维数组的方式的时候要注意,当遇到图形中心是一个时会出现死循环,需要特殊处理一下。如图:

2、测试数据:测试只有1个元素、只有1列元素、边长为奇数的正方形、边长偶数的正方形。

3、超时有可能是因为出现了死循环,而不一定是复杂度不对。

4、方法,设顶点为(x,y),边长为m、n。填充一圈后变为顶点(x+1, y+1),边长变为m - 2, n - 2。

A1105. Spiral Matrix的更多相关文章

  1. PAT甲级——A1105 Spiral Matrix【25】

    This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasi ...

  2. PAT_A1105#Spiral Matrix

    Source: PAT A1105 Spiral Matrix (25 分) Description: This time your job is to fill a sequence of N po ...

  3. [LeetCode] Spiral Matrix II 螺旋矩阵之二

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

  4. [LeetCode] Spiral Matrix 螺旋矩阵

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

  5. LeetCode - 54. Spiral Matrix

    54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...

  6. 【leetcode】Spiral Matrix II

    Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...

  7. 【leetcode】Spiral Matrix II (middle)

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

  8. 59. Spiral Matrix && Spiral Matrix II

    Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...

  9. LeetCode:Spiral Matrix I II

    Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...

随机推荐

  1. RPC框架-RMI、RPC和CORBA的区别

    关键词:RMI RPC CORBA简 介:本篇文章重点阐述RMI,附带介绍RPC和CORBA Java远程方法调用(Java RMI)是一组实现了远程方法调用(rmi)的API. java RMI是远 ...

  2. 工作效率提升之Eclipse篇(1):干掉烦人的xml文件的validation

    每次启动maven项目,都会有一堆烦人的xml文件的validation,一旦网络较慢,项目重新启动的时候,这些多余的验证纯属浪费时间. Eclipse上取消validation的方法: 1.菜单[W ...

  3. mycat - 全局序列

    解决主键冲突问题:例如id自增的order表,如果分布式情况下不处理的话,当每个表的第一条数据id都是1. 怎么确保id唯一呢? 解决办法: 1.本地文件(不推荐) 2.数据库方式(推荐) 3.时间戳 ...

  4. 4面向对象(OOP)

    学习线路 初学: 熟悉语法 进阶: 1.23种设计模式 2.6中开发原则 高级: 1.优化 2.架构 3.安全 概念 类:一类具有相同特性的事物的抽象描述,用一个java类表示. 成员变量:抽取的属性 ...

  5. ArcGIS 中UniqueValueRenderer和SimpleRenderer的异同点

    唯一值渲染器:UniqueValueRenderer用符号表示一组有匹配属性的图形,这通常用于名词或字符串数据.SimpleRenderer是使用单一符号进行着色分类,不涉及对要素的数据进行处理.这种 ...

  6. delphi中退出是弹出让你确定的几种确定对话框怎么写?

    procedure TForm2.btn1Click(Sender: TObject); begin if MessageBox(Handle, '你确定要退出系统吗?', '信息提示', MB_OK ...

  7. 今天开始学习模式识别与机器学习Pattern Recognition and Machine Learning (PRML),章节5.1,Neural Networks神经网络-前向网络。

    话说上一次写这个笔记是13年的事情了···那时候忙着实习,找工作,毕业什么的就没写下去了,现在工作了有半年时间也算稳定了,我会继续把这个笔记写完.其实很多章节都看了,不过还没写出来,先从第5章开始吧, ...

  8. 前后端进行数据交互时候 要优先考虑json格式即简直对形式,[{}] 列表形式 等便于操作的数据结构

    前后端进行数据交互时候 要优先考虑json格式即简直对形式,[{}] 列表形式 等便于操作的数据结构

  9. java 中的包概念

    Java 中的包package, 就是电脑中的文件夹.我们平时在工作中,文件太多时,都会新建文件夹进行分类管理,java 中的包也是类似的道理,当我们的类太多时,也需要进行分类管理,这时我们就会把类文 ...

  10. loj6270

    #6270. 数据结构板子题 sol:对于一个询问L,R,Limit,答案就是所有长度小于R-l+1的线段-长度小于Limit的线段-左端点在L左边的线段-右端点在R右边的线段,求这个东西 后面两个东 ...