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 nsatisfy 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 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
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn = ; //数字不能太大
int matrix[maxn][maxn],A[maxn]; bool cmp(int a,int b){
return a > b;
} int main(){
int N;
scanf("%d",&N);
for(int i = ; i < N; i++){
scanf("%d",&A[i]);
}
if(N == ){
printf("%d",A[]);
return ;
}
sort(A,A+N,cmp);
int m = (int)ceil(sqrt(1.0*N));
while(N % m != ) m++; //除不整的时候m++
int n = N / m, i = , j = , now = ;
int U = , D = m, L = , R = n;
while(now < N){
while(now < N && j < R){
matrix[i][j] = A[now++];
j++;
}
while(now < N && i < D){
matrix[i][j] = A[now++];
i++;
}
while(now < N && j > L){
matrix[i][j] = A[now++];
j--;
}
while(now < N && i > U){
matrix[i][j] = A[now++];
i--;
}
U++,D--,L++,R--;
i++,j++;
if(now == N - ){
matrix[i][j] = A[now++];
}
}
for(int i = ; i <= m; i++){
for(int j = ; j <= n; j++){
printf("%d",matrix[i][j]);
if(j < n) printf(" "); //j < n,不是j < n - 1
else printf("\n");
}
}
return ;
}

1105 Spiral Matrix(25 分)的更多相关文章

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

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

  2. 1105. Spiral Matrix (25)

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

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

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

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

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

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

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

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

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

  7. 1105 Spiral Matrix

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

  8. PAT 甲级 1105 Spiral Matrix

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

  9. PAT 1105 Spiral Matrix

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

随机推荐

  1. xdebug浏览器调试参数

    XDEBUG_SESSION_START=phpstorm-xdebug 找到对应PHP版本的 Xdebug ,后面带 TS 的为线程安全,本机环境为 win7 64 + php-5.5.1-Win3 ...

  2. encodeURI,encodeURIComponent编码

    encodeURI().encodeURIComponent().decodeURI().decodeURIComponent() URL编码 Global对象的encodeURI()和encodeU ...

  3. 51nod 1450 闯关游戏——期望dp

    题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1450 想了半天,不知道不能走的状态(即最后不足m个的状态)怎么办. ...

  4. js中this

    首先声明,我是小白,以下只是自己的简单理解. 先看下面的代码: (function () { console.log(this); })(); 毫无疑虑,输出的是window. 在看下面代码: (fu ...

  5. ss2

    一. *** 服务端配置 1. 在命令行窗口输入下面4行命令并回车执行 yum -y update yum install -y python-setuptools && easy_i ...

  6. JavaScript之JMap

    在JavaScript中我们利用function类定义类在类的内部我们用var 定义私有变量 私有函数在类的内部我们用this 定义公有变量(1)定义一个类 function JMap() { var ...

  7. Jenkins搭建Nodejs自动化测试

    一.安装Jenkins(Windows) 1. 在Jenkins官网(https://jenkins.io/)下载安装包,解压并安装 2. 安装完成后,会自动打开一个页面,根据提示在安装目录下找到随机 ...

  8. Jquery隐藏相同name的div

    $("div:[name=divName]").hide(); divName(自己div的Name)

  9. ES6之箭头函数中的this

    在讲箭头函数中的this之前我们先介绍一下普通函数中的this.      普通函数中的this: (1)this指向它的直接调用者 (2)默认的,非严格模式下,没找到直接调用者则指向window ( ...

  10. es6基础系列四--字符串的拓展

    1 for...of 字符串的遍历接口 for(let i of "abc"){ console.log(i); } // a // b // c 2 includes 是否包含某 ...