题意:
输入一个正整数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分)的更多相关文章

  1. PAT甲级——1105 Spiral Matrix (螺旋矩阵)

    此文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90484058 1105 Spiral Matrix (25 分) ...

  2. PAT 甲级 1105 Spiral Matrix

    https://pintia.cn/problem-sets/994805342720868352/problems/994805363117768704 This time your job is ...

  3. 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 ...

  4. 1105. Spiral Matrix (25)

    This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasi ...

  5. PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习

    1020 Tree Traversals (25分)   Suppose that all the keys in a binary tree are distinct positive intege ...

  6. PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)

    1146 Topological Order (25 分)   This is a problem given in the Graduate Entrance Exam in 2018: Which ...

  7. PAT 甲级 1071 Speech Patterns (25 分)(map)

    1071 Speech Patterns (25 分)   People often have a preference among synonyms of the same word. For ex ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. UI自动化测试的Page Object模式

    在UI级的自动化测试框架中,当页面样式改变或者页面元素属性改变,那么代码也要随之进行修改,如何做到高效快速的修改代码来适应这些改变呢,这个时候可以引入Page Object模式,也是页面对象设计模式. ...

  2. Linux下搭建PHP环境的参考文章小记

    之前也在Linux上安装过两次,但是当我再次需要安装时,还是很多不懂的地方,于是记下此篇,以防下次再费经心思找到不合适的文章,瞎折腾. 通过参考这几篇文章,成功的安装好了自己的PHP(LNMP)环境. ...

  3. C++——多态性

    多态是指发出同样的消息被不同类型的对象接收时有可能导致完全不同的行为: 多态的实现:函数重载:运算符重载:虚函数 为什么需要重载运算符? 在C++没有复数运算,进行复数运算之前我们要事先写一个复数类, ...

  4. IDEA构建maven项目生成的文件详解

    IDEA构建的maven+springBoot项目结构如下: 1. .gitignore:分布式版本控制系统git的配置文件,意思为忽略提交 在 .gitingore 文件中,遵循相应的语法,即在每一 ...

  5. Centos7安装python3.6.5

    安装python3.6.5,原来的python2.7.5并存 准备环境: 登录你的linux虚拟机或者云服务器,进入命令行界面如下图: 进入这样的窗口就是远程登录成功,我这里使用的是远程连接工具xsh ...

  6. JavaScript的jQuery

    JavaScript的jQuery 不通过JavaScript的原生代码,如document.getElementById("") 而是通过jQuery的$符号选择器. jQuer ...

  7. Consider using the `--user` option or check the permissions.

    安装pip install C:\Users\道路\Documents\EGDownloads\pip-1.0.tar.gz 报错:Consider using the `--user` option ...

  8. Atcoder Beginner Contest 156E(隔板法,组合数学)

    #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; ; ; long long fac[N] ...

  9. Redis 数据结构的底层实现 (一) RealObject,embstr,sds,ziplist,quicklist

    一.realObject Redis使用 string list zset hash set 五大数据类型来存储键和值.在每次生成一个键值对时,都会生成两个对象,一个储存键一个储存值.redis定义了 ...

  10. flask入门(一)

    flask是一个轻量级的框架,据说跟django跟比是真的轻. 首先要先配置一个虚拟环境,flask项目需要在那个虚拟环境里运行,这里需要用的venv库实在python3里的标准库,不过有的linux ...