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. 模态框 modal data-toggle data-target

    模态框 modal data-toggle data-target   1. Data-*属性 模态框(modal) 触发事件(data-toggle) 触发对象data-target(ID 或类) ...

  2. mybatis插入数据并返回自增Id

    上图mybatis的写法,在xxxMapper.xml中: 加入:useGeneratedKeys="true" keyProperty="applyId" k ...

  3. day 7-17 多表查询

    一. 准备表 #部门表 create table dep( id int, name varchar(20) ); #员工表 create table emp( id int primary key ...

  4. .Net在操作mysql查询的时候出现“: Unknown column 'UserName' in 'where clause'”错误

    今天使用.Net操作mysql查询的时候,如果加上条件查询的时候就会出现 Unknown column 'UserName' in 'where clause'这个错,不加条件直接select * f ...

  5. linux不同终端的操作是如何在messages日志中区分的

    今天在定位一个问题时,查看message日志,需要知道message日志中的记录分别是哪个Xterm终端操作的.比较了半天才发现原来日志中可以通过pts来进行区分.如下所示: --12T15:::|n ...

  6. 从git中删除 .idea 目录

    将.idea目录加入ignore清单: $ echo '.idea' >> .gitignore   从git中删除idea: $ git rm —cached -r .idea 3 将. ...

  7. python 网络编程 IO多路复用之epoll

    python网络编程——IO多路复用之epoll 1.内核EPOLL模型讲解     此部分参考http://blog.csdn.net/mango_song/article/details/4264 ...

  8. css多列居中

    https://jingyan.baidu.com/article/36d6ed1f67d58f1bcf488393.html

  9. String 常见的十种方法!

    public class ZiFuChuan { public static void main(String[] args) { ZiFuChuanFangFa f=new ZiFuChuanFan ...

  10. Java使用RabbitMQ之消息确认(confirm模板)

    RabbitMQ生产者消息确认Confirm模式,分为普通模式.批量模式和异步模式,本次举例为普通模式. 源码: package org.study.confirm4; import com.rabb ...