https://pintia.cn/problem-sets/994805342720868352/problems/994805363117768704

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 <bits/stdc++.h>
using namespace std; const int maxn = 1e5 + 10;
int N;
int num[maxn];
int ans[1010][1010]; int main() {
scanf("%d", &N);
for(int i = 0; i < N; i ++)
scanf("%d", &num[i]);
sort(num, num + N);
for(int i = 0; i < N / 2; i ++)
swap(num[i], num[N - i - 1]); int line, row;
for(row = sqrt((double)N); row >= 1; row --) {
if(N % row == 0) {
line = N / row;
break;
}
} int up = 0, down = line - 1, left = 0, right = row - 1, cnt = 0;
while(true) {
for(int j = left; j <= right; j ++) ans[up][j] = num[cnt ++];
if(++ up > down) break;
for(int i = up; i <= down; i ++) ans[i][right] = num[cnt ++];
if(-- right < left) break;
for(int j = right; j >= left; j --) ans[down][j] = num[cnt ++];
if(-- down < up) break;
for(int i = down; i >= up; i --) ans[i][left] = num[cnt ++];
if(++ left > right) break;
} for(int i = 0; i < line; i ++) {
for(int j = 0; j < row; j ++) {
printf("%d", ans[i][j]);
printf("%s", j != row - 1 ? " " : "\n");
}
}
return 0;
}

  中午好!

PAT 甲级 1105 Spiral Matrix的更多相关文章

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

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

  2. PAT甲级——A1105 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[模拟][螺旋矩阵][难]

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

  4. PAT 1105 Spiral Matrix

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

  5. 1105. Spiral Matrix (25)

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

  6. 1105 Spiral Matrix

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

  7. 1105 Spiral Matrix(25 分)

    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 (25分)

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

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

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

随机推荐

  1. Jenkins RCE(CVE-2018-1000861)

    先说通过IDEA利用JPDA远程调试tomcat程序 在catalina.sh添加,或者catalina.bat内容不动用如下命令开启,默认是开启8000端口 set JAVA_OPTS=-Xdebu ...

  2. WorldWind源码剖析系列:四叉树瓦片类QuadTile

    四叉树瓦片类QuadTile提供了对影像和地形数据的四叉树访问模型.该类的类图如下. 四叉树瓦片类QuadTile提供的主要字段.属性和方法简要描述如下: public QuadTileSet Qua ...

  3. Windows下安装Tensorflow—GPU版本

    https://blog.csdn.net/weixin_39290638/article/details/80045236

  4. NodeHandles

    os::NodeHandle类有两个作用: 第一.它在roscpp程序内提供了一种RAII(Resource Acquisition Is Initialization)类型式启动和关闭内部节点的方法 ...

  5. Android给拼接好的Bitmap加上个性化边框

    在上一节中将到将若干张图片拼接成为一张图片.但是这种简单的操作往往不能满足实际的需求,有时我们会需要给图片添加上个性化的边框,来更好的展示图片. 下面就讲一下在图片拼接后如何给bitmap添加边框. ...

  6. 第3章 如何用DAP仿真器下载程序

    第3章     如何用DAP仿真器下载程序 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/f ...

  7. LIN、CAN、FlexRay、MOST,三分钟搞明白四大汽车总线

    LIN.CAN.FlexRay.MOST,三分钟搞明白四大汽车总线 2016-09-21 13:09 汽车中的电子部件越来越多,光是ECU就有几十个,这么多的电子单元都要进行信息交互.传统的点对点通信 ...

  8. 1.5《想成为黑客,不知道这些命令行可不行》(Learn Enough Command Line to Be Dangerous)——清屏

    使用命令行时,使用clear命令清除屏幕非常方便: $ clear 键盘简写为^L.(Ctrl + L) 同样地,使用完终端当前窗口或标签页,使用exit命令退出进程: $ exit 键盘简写为^D ...

  9. 2017-2018-2 20155224『网络对抗技术』Exp5:MSF基础应用

    基础问题回答 用自己的话解释什么是exploit,payload,encode? exploit就相当于是载具,将真正要负责攻击的代码传送到靶机中,我觉得老师上课举的火箭和卫星的例子非常形象,火箭只是 ...

  10. [SHOI2012]随机树[期望dp]

    题意 初始 \(1\) 个节点,每次选定一个叶子节点并加入两个儿子直到叶子总数为 \(n\),问叶子节点深度和的平均值的期望以及最大叶子深度的期望. \(n\leq 100\) . 分析 对于第一问, ...