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×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 10^​4. 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<iostream>
#include<vector>
#include<math.h>
#include<algorithm>
using namespace std;
bool cmp(const int& a, const int& b){
return a>b;
}
int main(){
int N, k;
cin>>N;
for(k=sqrt((double)N); k>=1; k--)
if(N%k==0)
break;
int row=N/k, col=k;
int n=row/2;
vector<int> vi(N,0);
for(int i=0; i<N; i++)
cin>>vi[i];
sort(vi.begin(), vi.end(), cmp);
vector<vector<int>> graph(row,vector<int>(col,0));
int cnt=0;
for(int i=0; i<=n; i++){
for(int j=i; j<col-i&&cnt<N; j++)
graph[i][j]=vi[cnt++];
for(int j=i+1; j<row-i&&cnt<N; j++)
graph[j][col-i-1]=vi[cnt++];
for(int j=col-i-2; j>=i&&cnt<N; j--)
graph[row-i-1][j]=vi[cnt++];
for(int j=row-2-i; j>=i+1&&cnt<N; j--)
graph[j][i]=vi[cnt++];
}
for(int i=0; i<row; i++){
for(int j=0; j<col; j++)
j==0?cout<<graph[i][j]:cout<<" "<<graph[i][j];
cout<<endl;
}
return 0;
}

PAT 1105 Spiral Matrix的更多相关文章

  1. PAT 1105 Spiral Matrix[模拟][螺旋矩阵][难]

    1105 Spiral Matrix(25 分) This time your job is to fill a sequence of N positive integers into a spir ...

  2. PAT甲级——1105 Spiral Matrix (螺旋矩阵)

    此文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90484058 1105 Spiral Matrix (25 分) ...

  3. PAT 甲级 1105 Spiral Matrix

    https://pintia.cn/problem-sets/994805342720868352/problems/994805363117768704 This time your job is ...

  4. 1105. Spiral Matrix (25)

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

  5. 1105 Spiral Matrix

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

  6. 1105 Spiral Matrix(25 分)

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

  7. PAT甲题题解-1105. Spiral Matrix (25)-(模拟顺时针矩阵)

    题意:给定N,以及N个数.找出满足m*n=N且m>=n且m-n最小的m.n值,建立大小为m*n矩阵,将N个数从大到下顺时针填入矩阵中. #include <iostream> #in ...

  8. PAT (Advanced Level) 1105. Spiral Matrix (25)

    简单模拟. #include<cstdio> #include<cstring> #include<cmath> #include<map> #incl ...

  9. 【PAT甲级】1105 Spiral Matrix (25分)

    题意:输入一个正整数N(实则<=1e5),接着输入一行N个正整数(<=1e4).降序输出螺旋矩阵. trick: 测试点1,3运行超时原因:直接用sqrt(N)来表示矩阵的宽会在N是素数时 ...

随机推荐

  1. uboot流程分析--修改android启动模式按键【转】

    本文转载自:http://blog.csdn.net/dkleikesa/article/details/9792747 本人用的android平台用的bootloader用的是uboot,貌似大多数 ...

  2. 启动Tomcat任何程序都报错

    启动Tomcat出现: Cannot publish to the server because it is missing its runtime environment. 可能是servers中不 ...

  3. js【面向过程编程】、好、 【init()、 GetData()、 bindData()、bindDom、 bindEvent()、buyProduct()、AddProductToCart()】*****************

    1. 一般页面开发方式 [可读性差.可维护性差]------初级开发工程师 一般页面编写方法 var name = 'iphone8' var description = '手机中的战斗机 ' var ...

  4. [SCOI 2010] 连续攻击游戏

    [题目链接] https://www.luogu.org/problemnew/show/P1640 [算法] 二分图匹配 实现时需要常数优化和特判 [代码] //code by byf and lm ...

  5. Linux基本命令 文件管理 上部

    第1章 Linux入门相关 目录基本知识 Linux一切从根开始 倒挂的树形结构 对路径与相对路径 绝对路径: 从根开始的路径 比如:/oldboy  /data 相对路径: 没有从根开始的路径 比如 ...

  6. js产生随机数教程

    <script>   function GetRandomNum(Min,Max){   var Range = Max - Min;   var Rand = Math.random() ...

  7. selenium3 + python - action_chains源码分析

    ActionChains简介 actionchains是selenium里面专门处理鼠标相关的操作如:鼠标移动,鼠标按钮操作,按键和上下文菜单(鼠标右键)交互.这对于做更复杂的动作非常有用,比如悬停和 ...

  8. 【转】深入理解Java多态原理

    之前一直知道多态是什么东西,平时敲代码也经常用到多态,但一直没有真正了解多态底层的运行机制到底是怎么样的,这两天才研究明白点,特地写下来,跟各位同学一起进步,同时也希望各位大神指导和指正. 多态的概念 ...

  9. linux 查看 cpu

    如何获得CPU的详细信息: linux命令:cat /proc/cpuinfo 用命令判断几个物理CPU,几个核等: 逻辑CPU个数:# cat /proc/cpuinfo | grep " ...

  10. 【知识总结】快速傅里叶变换(FFT)

    这可能是我第五次学FFT了--菜哭qwq 先给出一些个人认为非常优秀的参考资料: 一小时学会快速傅里叶变换(Fast Fourier Transform) - 知乎 小学生都能看懂的FFT!!! - ...