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. c++ 编译器会绕过拷贝构造函数

    C++ primer P442 P447:在拷贝初始化过程中,编译器可以跳过拷贝构造函数,直接创建对象.即,编译器允许将下面的代码 "; //1 改写为 "); //2 由于str ...

  2. Android分包MultiDex原理详解

    MultiDex的产生背景 当Android系统安装一个应用的时候,有一步是对Dex进行优化,这个过程有一个专门的工具来处理,叫DexOpt.DexOpt的执行过程是在第一次加载Dex文件的时候执行的 ...

  3. Android 触摸提示音【转】

    本文转载自:http://blog.csdn.net/Jin_HeZai/article/details/46791567 近期任务,涉及Android触摸提示音. 首先,定位源码目标.很显然的,在原 ...

  4. go语言笔记——map map 默认是无序的,不管是按照 key 还是按照 value 默认都不排序

    示例 8.1 make_maps.go package main import "fmt" func main() { var mapLit map[string]int //va ...

  5. Datatable转换为Json 的方法

    /// <summary>         /// Datatable转换为Json         /// </summary>        /// <param n ...

  6. windows 7系统下安装SQL Server 2005图文教程

    由于工作需要,今天要在电脑上安装SQL Server 2005.以往的项目都是使用Oracle,MS的数据库还真的没怎么用过,安装Oracle已经轻车熟路,但装SQL Server好像还有点小麻烦,所 ...

  7. Network(Tarjan+LCA)

    http://poj.org/problem?id=3417 元宵节+情人节晚上刷的题,纪念一下.. 题意:给出n个点,m条边,然后Q个询问,每次询问输入一条边,输出加入此边后桥的个数.. #incl ...

  8. [Swift通天遁地]二、表格表单-(17)制作在表单左侧添加单选和复选组件的表单行

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  9. ThreadLocal原理及用法详解

    背景 一直以来对ThreadLocal用法模棱两可,不知道怎么用今天好好研究了下给大家分享下. 1.讲解ThreadLocal之前先回顾下什么是取模.x^y.弱引用. 1. 取模运算实际上是计算两数相 ...

  10. Spring-Security-OAuth2微信网页授权

    @Controller public class Controller1 { @Autowired private OAuth2ClientContext context; @Bean @Scope( ...