Source:

PAT A1105 Spiral Matrix (25 分)

Description:

This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasing order. A spiral matrix is filled in from the first element at the upper-left corner, then move in a clockwise spiral. The matrix has m rows and n columns, where m and n satisfy the following: m×nmust 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 1. 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

Keys:

  • 简单模拟

Attention:

  • 基本思路就是把序列按照从大到小的顺序,依次填入矩阵中;
  • 直接开1e4的矩阵会超出内存,而且样例中存在m较大,n较小的情况,因此用一维数组代替二维数组
  • 循环内的四个for循环需要判断pt<N

Code:

 /*
Data: 2019-06-08 16:12:47
Problem: PAT_A1105#Spiral Matrix
AC: 01:05:29 题目大意:
序列从大到小,顺时针放入矩阵中
*/
#include<functional>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int M=1e4+;
int matrix[M], a[M]; int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif int N,n,m;
scanf("%d", &N);
for(int i=; i<N; i++)
scanf("%d", &a[i]);
for(int i=(int)sqrt((double)N); i>=; i--)
{
if(N%i == )
{
n = i;
m = N/i;
break;
}
}
sort(a,a+N,greater<int>());
fill(matrix,matrix+M,);
int pt=,c=,r=,R=m,C=n;
while(pt < N)
{
for(int i=c; i<=n && pt<N; i++)
matrix[C*r-+i-]=a[pt++];
r++;
for(int i=r; i<=m && pt<N; i++)
matrix[C*i-+n-]=a[pt++];
n--;
for(int i=n; i>=c && pt<N; i--)
matrix[C*m-+i-]=a[pt++];
m--;
for(int i=m; i>=r && pt<N; i--)
matrix[C*i-+c-]=a[pt++];
c++;
}
for(int i=; i<=R; i++)
for(int j=; j<=C; j++)
printf("%d%c", matrix[C*i-+j-], j==C?'\n':' '); return ;
}

PAT_A1105#Spiral Matrix的更多相关文章

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

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

  2. [LeetCode] Spiral Matrix 螺旋矩阵

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

  3. LeetCode - 54. Spiral Matrix

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

  4. 【leetcode】Spiral Matrix II

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

  5. 【leetcode】Spiral Matrix II (middle)

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

  6. 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 ...

  7. 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 ...

  8. Java for LeetCode 059 Spiral Matrix II

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

  9. Leetcode#59 Spiral Matrix II

    原题地址 相比于Spiral Matrix(参见这篇文章)要简单一些,因为是方阵,所以代码简洁一些. 注意当n是奇数的时候,中心小块要单独赋值(代码21行) 代码: vector<vector& ...

随机推荐

  1. N天学习一个linux命令之ps

    ps命令 用途 显示系统进程信息 用法 ps [options] 常用选项 选项有三种风格,这里是指Unix风格 (Unix,BSD,GNU LONG OPTIONS) 简单刷选类 -A, -e 显示 ...

  2. Spark 颠覆 MapReduce 保持的排序记录

    在过去几年,Apache Spark的採用以惊人的速度添加着,通常被作为MapReduce后继,能够支撑数千节点规模的集群部署. 在内存中数 据处理上,Apache Spark比MapReduce更加 ...

  3. Linux vim 入门 配置 及 使用初步

    网上能够找到的,关于VI的教程,更是多为能吓死人的上百页说明.事实上, 从我个人的实践看,全然不须要如此夸张.要完毕最主要的编辑.仅仅要熟悉几个命令,就是把VIM用得非常好. 这里就列举一下: Esc ...

  4. Hive权限之审计

    因为在生产环境中大量使用hive.而hive的权限又较弱,假设可以记录全部hive操作,在增强安全性的同一时候,还可以统计hive表的使用频率:同一时候假设可以记录hql的開始和结束时间,则可以找出系 ...

  5. 改动mysqlpassword

    1.假设没有password,则 mysql -u root mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass'); ...

  6. spring与springboot中,如何在static方法里使用自动注入的属性

    第一步:写注解@Component 使当前类成为一个bean对象.(@Controller,@service都行) 第二步:写个static的变量 第三步:写个@PostConstruct注解注解注释 ...

  7. HDU 4099 Revenge of Fibonacci Trie+高精度

    Revenge of Fibonacci Problem Description The well-known Fibonacci sequence is defined as following: ...

  8. C# winform KeyPress 事件中对应的数字

    C#  winform KeyPress 事件中对应的数字所有e.KeyChar值的意思 常用ASCII码表 你自己看看应该就明白了 键盘 ASCII码 键盘 ASCII码 ESC 27 7 55 S ...

  9. 深度学习必备:随机梯度下降(SGD)优化算法及可视化

    补充在前:实际上在我使用LSTM为流量基线建模时候,发现有效的激活函数是elu.relu.linear.prelu.leaky_relu.softplus,对应的梯度算法是adam.mom.rmspr ...

  10. AMD 与 CMD 区别

    作者:玉伯链接:https://www.zhihu.com/question/20351507/answer/14859415来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出 ...