【PAT甲级】1105 Spiral Matrix (25分)
题意:
输入一个正整数N(实则<=1e5),接着输入一行N个正整数(<=1e4)。降序输出螺旋矩阵。
trick:
测试点1,3运行超时原因:直接用sqrt(N)来表示矩阵的宽会在N是素数时出错,直接扫一遍找一个最大因子即可,简单粗暴。
AAAAAccepted code:
#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
int a[];
int ans[][];
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin>>n;
for(int i=;i<=n;++i)
cin>>a[i];
int y=;
for(int i=;i*i<=n;++i)
if(n%i==)
y=i;
int x=n/y;
sort(a+,a++n);
int top=,bottom=x,left=,right=y;
int temp=n;
int flag=;
while(temp){
if(flag==){
for(int i=left;i<=right;++i)
ans[top][i]=a[temp--];
++top;
flag=;
}
else if(flag==){
for(int i=top;i<=bottom;++i)
ans[i][right]=a[temp--];
--right;
flag=;
}
else if(flag==){
for(int i=right;i>=left;--i)
ans[bottom][i]=a[temp--];
--bottom;
flag=;
}
else if(flag==){
for(int i=bottom;i>=top;--i)
ans[i][left]=a[temp--];
++left;
flag=;
}
}
for(int i=;i<=x;++i){
for(int j=;j<=y;++j){
cout<<ans[i][j];
if(j<y)
cout<<" ";
}
if(i<x)
cout<<"\n";
}
return ;
}
【PAT甲级】1105 Spiral Matrix (25分)的更多相关文章
- PAT甲级——1105 Spiral Matrix (螺旋矩阵)
此文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90484058 1105 Spiral Matrix (25 分) ...
- PAT 甲级 1105 Spiral Matrix
https://pintia.cn/problem-sets/994805342720868352/problems/994805363117768704 This time your job is ...
- 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 ...
- 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 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习
1020 Tree Traversals (25分) Suppose that all the keys in a binary tree are distinct positive intege ...
- PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)
1146 Topological Order (25 分) This is a problem given in the Graduate Entrance Exam in 2018: Which ...
- PAT 甲级 1071 Speech Patterns (25 分)(map)
1071 Speech Patterns (25 分) People often have a preference among synonyms of the same word. For ex ...
- PAT 甲级 1063 Set Similarity (25 分) (新学,set的使用,printf 输出%,要%%)
1063 Set Similarity (25 分) Given two sets of integers, the similarity of the sets is defined to be ...
- PAT 甲级 1059 Prime Factors (25 分) ((新学)快速质因数分解,注意1=1)
1059 Prime Factors (25 分) Given any positive integer N, you are supposed to find all of its prime ...
随机推荐
- sqli-labs less-1 --> less-4
Less-1 (报错注入) 因为第一次做这些题,不太了解,所以$sql下加上echo "$sql<br>";能更明显的看出具体的输入 1.判断是否存在注入点 当输入? ...
- MapReduce第一个项目 WordCount
参考自林子雨大数据教学: http://dblab.xmu.edu.cn/blog/hadoop-build-project-using-eclipse/ 创建一个文件夹:放入一个文本文件:填 ...
- 我的翻译--GSMem:通过GSM频率从被物理隔离的计算机上窃取数据
抽象概念 AG网络是指在物理上与公共互联网断开的网络.虽然近几年人们验证了入侵这类网络系统的可行性,但是从这种网络上获取数据仍然是一个有挑战的任务.在本文中,我们介绍GSMem,它是一个可以在蜂窝数据 ...
- 常见通用框架的理解(Redis,Zookeeper,Thrift)
redis 主要功能是内存版的Hashta zookeeper 主要功能是分布式中的全局变量. thrift 跨平台的Client和Server通信架构. taskengine用于启动定时任务和查看 ...
- 论如何用python发qq消息轰炸虐狗好友
因为我的某个好友在情人节的时候秀恩爱,所以我灵光一闪制作了qq消息轰炸并记录了下来. PS:另外很多人在学习Python的过程中,往往因为遇问题解决不了或者没好的教程从而导致自己放弃,为此我整理啦从基 ...
- 关于“教室派”APP的使用报告和相关建议
教室派APP能够很好的解决学生查询各教室占用情况这一问题,使用起来非常方便.用户可根据需要选取星期来查询不同教学楼教室使用情况. 编辑课表是其附带功能,但通过使用发现手动编辑课表效率太低,建议开发者加 ...
- AcWing 894. 拆分-Nim游戏
#include <cstring> #include <iostream> #include <algorithm> #include <unordered ...
- HDR视频生态圈追踪
截止目前,HDR视频生态圈已经产生了巨大的变化.本文将更新旧有的HDR生态圈范围,并更清晰地描述当前HDR视频生态圈.本文译自The HDR video ecosystem tracker,原作者为 ...
- 根下var目录迁移
问题 var目录挂载在根目录下,占用/分区的空间,如果/var/log日志空间过大,将导致/分区空间满,导致系统无法使用. 解决方案 在硬盘上划分单独的分区,挂载到/var目录,并迁移/var目录下的 ...
- 代码反向生成数据库注释更新sql
原理 通过反射实体所在程序集,得到枚举值列表,再通过sql获取数据库表结构,两者拼接成sql. 规范 实体枚举字段最好也加上Description特性,方便多次更新: 代码 实体定义 public p ...