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. php大流量高并发解决方案

    一.硬件 提升硬件,影响因素有: 带宽-硬盘读写速度-内存大小-cpu处理速度   二.软件 反向代理负载均衡 mysql : 1.优化你的sql和索引 2.加缓存,memcached,redis 3 ...

  2. php可逆加密解密

    函数: function encrypt($data, $key) { $prep_code = serialize($data); $block = mcrypt_get_block_size('d ...

  3. 基于TP5使用Websocket框架之GatewayWorker开发电商平台买家与卖家实时通讯

    https://www.cnblogs.com/wt645631686/p/7366924.html 前段时间公司提了一个新的需求,在商品的详情页要实现站内买家和商品卖家实时通讯的功能以方便沟通促成交 ...

  4. VA_X安装

    1,下载VA_X安装包: 地址:https://pan.baidu.com/s/16v5RFX2apjqvDy1Jw8A6Dg 密码:tmv0 解压后显示: 第一步: 第二步:将这个解压文件下的VA_ ...

  5. 利用jenkins打造通过自定义参数更新svn 指定文件任务

    jenkin可以执行很多构建任务,有时候我们需要在执行构成中同构shell对服务器进行操作而且还需要进行参数的传入 比如:我要利用svn进行本地代码的更新,单又不是所有代码的更新,只更新指定的1个或这 ...

  6. setattr

    setatt r给对象的属性赋值,若属性不存在,先创建再赋值 用法 setattr(object, name, values) object -- 对象. name -- 字符串,对象属性. valu ...

  7. k-center问题-学习

    k-center问题: In graph theory, the metric k-center or metric facility location problem is a combinator ...

  8. 编写陈旭,实现通过字符型变量创建boolean值,再将其转换为字符串输出,观察输出后的字符串与创建Boolean对象时给定的参数是否相等.

    主要涉及到基本数据类型与包装类的转换. Boolean类的tostring方法的作用,返回一个表示该布尔值的 String 对象 public class Test1 { public static ...

  9. day62

    一.组件 组件都具有模板,template new Vue()创建的是根组件 组件与实例一一对应,创建一个实例就是创建了一个组件,同理创建一个组件就相当于创建了一个实例 根组件的挂载点一般就是根组件的 ...

  10. CAN调度理论与实践分析

    CAN调度理论与实践分析 分布式嵌入式系统是当前嵌入式系统的重要发展方向,因为它能提供更强的性能,节约系统的总体成本.但是由于各单个节点必须有通信网络相连才能协调地工作,网络就成了关键部分,没有网络提 ...