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. influx测试——单条读性能很差,大约400条/s,批量写性能很高,7万条/s,总体说来适合IOT数据批量存,根据tag查和过滤场景,按照时间顺序返回

    测试准备 需要将InfluxDB的源码放入 go/src/github.com/influxdata 目录 单写测试代码(write1.go): package main import ( " ...

  2. JSP-Runoob:JSP 链接数据库

    ylbtech-JSP-Runoob:JSP 链接数据库 1.返回顶部 1. JSP 连接数据库 本教程假定您已经了解了 JDBC 应用程序的工作方式.在您开始学习 JSP 数据库访问之前,请访问 J ...

  3. codeforces round #427 div2

    A:读懂题,乘一下判断大小就行了 #include<bits/stdc++.h> using namespace std; int main() { int s, v1, v2, t1, ...

  4. MyBatis高级查询 一对一映射

    drop database if exists simple; create database simple; use simple; drop table if exists sys_user; c ...

  5. java replaceAll 忽略大小写

    public static void main(String[] args) { String temp=" CLASS_path : /aaabb/"; System.out.p ...

  6. zookeeper单机安装

    安装zookeeper步骤: 1,下载zookeeper http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.14/ 2,放到合适目录,解压 ...

  7. 洛谷P1726 上白泽慧音(Tarjan强连通分量)

    P1726 上白泽慧音 题目描述 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄.因此慧音决定换一个能够聚集最多人数的村 ...

  8. BZOJ 3998 后缀数组

    思路: 第一问 建出来后缀数组以后  前缀和一发n-sa[i]-ht[i]+1  二分 第二问 二分判断是带重复的第几 怎么判断呢   找到它  往后扫ht递减sum+=它   跟K判判 注意等于 加 ...

  9. [转]Android定时刷新UI界面----Handler

    本文转自:http://blog.csdn.net/macong01/article/details/7479266 本想做一个软件可以对UI界面进行定时更新,找了一些资料,先贴一个简单的定时更新界面 ...

  10. 附加数据库错误代码 - 5120【MSSQL】

    解决方法 数据库所在的文件夹右击打开属性 - 安全 - 给予Authenticated Users用户完全控制权限.然后再附加一次即可成功.