PAT 甲级 1105 Spiral Matrix
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 104. 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的更多相关文章
- PAT甲级——1105 Spiral Matrix (螺旋矩阵)
此文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90484058 1105 Spiral Matrix (25 分) ...
- 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 ...
- PAT 1105 Spiral Matrix[模拟][螺旋矩阵][难]
1105 Spiral Matrix(25 分) This time your job is to fill a sequence of N positive integers into a spir ...
- PAT 1105 Spiral Matrix
This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasi ...
- 1105. Spiral Matrix (25)
This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasi ...
- 1105 Spiral Matrix
This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasi ...
- 1105 Spiral Matrix(25 分)
This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasi ...
- 【PAT甲级】1105 Spiral Matrix (25分)
题意:输入一个正整数N(实则<=1e5),接着输入一行N个正整数(<=1e4).降序输出螺旋矩阵. trick: 测试点1,3运行超时原因:直接用sqrt(N)来表示矩阵的宽会在N是素数时 ...
- PAT甲题题解-1105. Spiral Matrix (25)-(模拟顺时针矩阵)
题意:给定N,以及N个数.找出满足m*n=N且m>=n且m-n最小的m.n值,建立大小为m*n矩阵,将N个数从大到下顺时针填入矩阵中. #include <iostream> #in ...
随机推荐
- ELK (Elasticsearch+Logstash+Kibana)部署
部署机器: 服务端:dev-server X.X.X.X ( logstash-1.5.4,elasticsearch-1.7.1,kibana-4.1.1 ) 客户端:dev-cli ...
- Docker技术入门与实战 第二版-学习笔记-9-Docker Compose 项目-1-举例说明
Docker Compose 是 Docker 官方编排(Orchestration)项目之一,负责快速在集群中部署分布式应用 Compose 通过一个配置文件来管理多个Docker容器,在配置文件中 ...
- kubernetes 资源请求和限制
1. spec: containers: - name: example resources: requests: ...
- oracle版本兼容问题
问题一描述:本机环境升级为vs2012升级TLS程序为framework4.0,本机ODAC为ODTwithODAC112030.本机为oracle10g本机程序生成成功,运行成功. 发布到服务器后, ...
- 【MEVN架构】mongodb+ express + vue + nodejs 搭建后台
前端技术栈:vue2 + vuex + vue-router + webpack + ES6/7 + less + element-ui 服务端技术栈:nodejs + express + mongo ...
- HDFS--大数据应用的基石
近些年,由于智能手机的迅速普及推动移动互联网技术的蓬勃发展,全球数据呈现爆发式的增长.2018年5月企鹅号的统计结果:互联网每天新增的数据量达2.5*10^18字节,而全球90%的数据都是在过去的两年 ...
- Verilog使用相对路径时应注意的问题
在Quartus编译环境下,使用include, fopen等文件操作指令时,会涉及到文件路径问题. 以 E:\quartus_project\sd_card_controller\rtl\sd_wb ...
- 20155216 Exp8 WEB基础实践
Exp8 WEB基础实践 实践内容 Apache环境配置 apache是kali下的web服务器,通过访问 ip地址+端口号+文件名称 打开对应的网页. 输入命令 vi /etc/apache2/po ...
- 20155334 《网络攻防》 Exp6 信息搜集与漏洞扫描
20155334 Exp6 信息搜集与漏洞扫描 一.实验问题回答 哪些组织负责DNS,IP的管理? 答:互联网名称与数字地址分配机构 ICANN ,该机构决定了域名和IP地址的分配.负责协调管理DNS ...
- Android开发——进程间通信之Messenger
0. 前言 不论是Android还是其他操作系统,都会有自己的IPC机制,所谓IPC(Inter-Process Communication)即进程间通信.首先线程和进程是很不同的概念,线程是CPU ...