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. PHP二维数组(或任意维数组)转换成一维数组的方法汇总(实用)

    目录 1 array_reduce函数法 2 array_walk_recursive函数法 3 array_map函数法 假设有下面一个二维数组: $user = array( '0' => ...

  2. object-fit 属性的用法介绍

    这个要在宽,高都是100%的情况下才能提现 object-fit 属性的用法介绍 fill(不保持纵横比缩放图片,使图片完全适应) contain(保持纵横比缩放图片,使图片的长边能完全显示出来) c ...

  3. java.io.FileNotFoundException关于使用Intellij Idea时系统找不到指定文件的解决方案

    第一种:Intellij Idea 这个智障编辑器 在用的时候 是你在这个web目录下的空文件夹他是不给你部署的 解决在空文件夹下面随便放个文件夹就行了 第二种:也是最笨的方法,但是有前提条件就是 你 ...

  4. 使用mysqlbinlog恢复数据

    前提:mysql数据库开启了binlog日志,并且有对应的日志文件 起因:今天由于同事对数据库的误操作不小心删除了一条数据 方法一:通过binlog日志文件恢复数据 通过mysqlbinlog恢复My ...

  5. windows git支持arc命令

    本文整理了在Windows系统上安装代码审查工具Arcanist的过程.目的是配合Phabricator进行代码review.配置成功后可使用arc diff命令来发起code review. 1.安 ...

  6. Jenkins+PowerShell持续集成环境搭建(一)前期准备

    0. 系统要求 Windows Server:本文章使用的为Windows Server 2012 JDK:本文章使用为JDK 1.8 Windows PowerShell:本文章使用为PowerSh ...

  7. Hadoop Brief

    Hadoop是一个由Apache基金会所开发的分布式系统基础架构. 用户可以在不了解分布式底层细节的情况下,开发分布式程序.充分利用集群的威力进行高速运算和存储. [1]  Hadoop实现了一个分布 ...

  8. HTTP协议那些事儿(Web开发补充知识点)

    HTTP协议 超文本传输协议(英文:HyperText Transfer Protocol,缩写:HTTP)是一种用于分布式.协作式和超媒体信息系统的应用层协议.HTTP是万维网的数据通信的基础. H ...

  9. ubuntu6.04安装

    一.在windows操作系统下准备ubuntu系统的安装盘 1. 下载ubuntu的ISO文件 这一步相对简单,网络上面有很多的链接下载.这里贴一个ubuntu的官方网站链接,可以下载到ubuntu ...

  10. 解决Error:com.intellij.util.indexing.StorageException

    删除 C:\Users\Nihaorz\.IntelliJIdea2017.1\system\compile-server 目录下的所有内容即可