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. 【转】IE内嵌google chrome frame解决浏览器兼容问题

    参考文献: http://www.pseudowired.com/2012/12/04/tomcat-http-header-manipulation/(html中自动添加使用chrome的heade ...

  2. centos7之Java开发环境构建

    CensOS7环境 我个人的博客环境如下: 希望这个教程可以帮助到linux新手朋友们或者其他在安装软件时遇到问题的朋友们 当然了,百度上也有很多类似这样的教程,我个人贴出来,一来为分享,二来以后自己 ...

  3. 关于 Spring AOP (AspectJ) 该知晓的一切

    关联文章: 关于Spring IOC (DI-依赖注入)你需要知道的一切 关于 Spring AOP (AspectJ) 你该知晓的一切 本篇是年后第一篇博文,由于博主用了不少时间在构思这篇博文,加上 ...

  4. Ubuntu安装Chromium浏览器

    今天介绍一下谷歌浏览器在ubuntu 系统环境下的安装步骤,1.在终端的窗口上输入: sudo wget http://www.linuxidc.com/files/repo/google-chrom ...

  5. day36

    今日内容 1.GIL解释器锁 2.GIL解释器锁与自定义锁 3.死锁现象与递归锁 4.信息量 5.Event 6.线程queue 1.GIL解释器锁 from multiprocessing impo ...

  6. gitlab webhook php exec 调用 shell 脚本。shell 脚本中调用 git pull 命令无法执行。

    情况如下: 我在ubuntu server 14.04 上面安装了gitlab,来托管项目代码.然后想通过gitlab的web hook 功能来做测试服务器代码自动化更新代码功能.现在遇到一个问题:就 ...

  7. HDU - 4118 Holiday&#39;s Accommodation

    Problem Description Nowadays, people have many ways to save money on accommodation when they are on ...

  8. Maltego——互联网情报聚合工具初探(转)

    有时候你可曾想过,从一个Email,或者Twitter,或是网站,甚至姓名等等,能找到一个人千丝万缕的联系,并把这些联系整合,利用起来?Maltego就是这样一款优秀而强大的工具.Maltego允许从 ...

  9. [Zlib]_[初级]_[使用zlib库压缩和解压STL string]

    场景 1.一般在使用文本json传输数据, 数据量特别大时,传输的过程就特别耗时, 因为带宽或者socket的缓存是有限制的, 数据量越大, 传输时间就越长. 网站一般使用gzip来压缩成二进制. 说 ...

  10. vector 去重

    1.使用unique函数: sort(v.begin(),v.end()); v.erase(unique(v.begin(), v.end()), v.end()); //unique()函数将重复 ...