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. kali 2016.2安装及配置

    之前安装过kali,现在换了台电脑重新安装一遍,顺便记录下来,因为面向新手所以会很详(luo)细(suo) 安装: 首先到官网去下载镜像文件:https://www.kali.org/download ...

  2. iframe-metamask

    iframe--require('iframe') higher level api for creating and removing iframes in browsers 用于创建或移除浏览器中 ...

  3. ap、map值计算

    ap:所有图片某一个类 map:所有图片所有类的ap的平均 以一个score为阈值,大于score的所有框是假定正确输出的所有预测,将这些框和gt匹配(iou大于某一个阈值认为匹配成功),得到当前sc ...

  4. CSS3页面布局方案

    CSS3页面布局方案 Web页面中的布局,在css3之前,主要使用float属性或者position属性进行页面中的简单布局,但是使用它们也存在一些缺点,比如两栏或者多栏中如果元素的内容高度不一致,则 ...

  5. Android 将系统的back键模拟成为home键的功能

    @Override public void onBackPressed() { Intent intent = new Intent(Intent.ACTION_MAIN); intent.setFl ...

  6. OpenShift-EFK日志管理

    1.准备工作 思路: 在OpenShift容器平台上以daemonset方式部署Fluentd收集各节点中的日志.更改其配置让日志输出到外部Elasticsearch中,最终通过Kibana展示. 资 ...

  7. 20155317王新玮《网络对抗》Exp2 后门原理与实践

    20155317王新玮<网络对抗>Exp2 后门原理与实践 一.实验内容 (1)使用netcat获取主机操作Shell,cron启动 (2)使用socat获取主机操作Shell, 任务计划 ...

  8. 全虚拟化和半虚拟化的区别 cpu的ring0 ring1又是什么概念? - 转

    http://www.cnblogs.com/xusongwei/archive/2012/07/30/2615592.html ring0是指CPU的运行级别,ring0是最高级别,ring1次之, ...

  9. JavaScript快速入门-ECMAScript本地对象(RexExp)

    一.概述 RegExp 对象表示正则表达式,它是对字符串执行模式匹配的强大工具. 正则表达式是由一个字符序列形成的搜索模式. 当你在文本中搜索数据时,你可以用搜索模式来描述你要查询的内容. 正则表达式 ...

  10. flask-login 整合 pyjwt + json 简易flask框架

    现在很多框架都实现前后端分离,主要为了适应以下几个目的: 1,前后端的分离,可以使前端开发和后端开发更加分工明确,而不是后端还需要在视图模板中加入很多{% XXXX %}标签 2,是为了适应跨域调用或 ...