题意:
输入一个正整数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. Sql注入之注入点类型和是否存在注入判断

    SQL注入之判断注入类型注入类型分为数字型和字符型和搜索型例如数字型语句:select * from table where id =3,则字符型如下:select * from table wher ...

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

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

  3. Wannafly Camp 2020 Day 1C 染色图 - 组合数学,整除分块

    定义一张无向图 G=⟨V,E⟩ 是 k 可染色的当且仅当存在函数 f:V↦{1,2,⋯,k} 满足对于 G 中的任何一条边 (u,v),都有 f(u)≠f(v). 定义函数 g(n,k) 的值为所有包 ...

  4. @Cacheable注解不生效原因

    因为@Cacheable注解应用了AOP动态代理,生成代理类,判断缓存中是否存在该key,如果不存在则调用被代理类的标有@Cachable注解的方法,否则不执行. 所以当类A的方法a调用方法b(标有@ ...

  5. 题解 【洛谷P1332】血色先锋队

    题面:https://www.luogu.org/problemnew/show/P1332 很容易看出,这是一个典型的广度优先搜索,也就是众多OIer口中的bfs. 先给各位普及一下bfs(部分来自 ...

  6. NW.js桌面应用开发(一)

    NWjs中文网 Electron中文网 一些需要了解的历史与特性,其实就是 NW.js 和 Electron的争议,建议还是亲自阅读一下各自的官网说明 1.下载SDK版工具 从淘宝NPM镜像下载,速度 ...

  7. 经常使用的cmd命令

    ASSOC 显示或修改文件扩展名关联.ATTRIB 显示或更改文件属性.BREAK 设置或清除扩展式 CTRL+C 检查.BCDEDIT 设置启动数据库中的属性以控制启动加载.CACLS 显示或修改文 ...

  8. laravel如何A表中包含B表中信息

    A表中如何包含B表中的信息 首先看A表的信息 接着看B表的信息 我的需求就是 A表字段name对应B表字段ream_name然后得到B表的对应主键ID要在A表中查询出来 发现问题就是查询出来的id和A ...

  9. HTML链接标签

    <a>超链接标签:常用属性: href:指定地址,要有HTTP协议.如果是本网站的html文件可以写路径 target:以什么方式打开 _self:在当前窗口打开(默认) _blank:新 ...

  10. 记manjaro图形驱动删除后的一次补救

    #一.前言 众所周知,NVIDIA的闭源驱动在Linux上的兼容性不是很好,再加上我不玩游戏,于是我就想卸载独显只留核显.我以为我装了独显和核显两种驱动,原本想直接删除独显驱动,没想到删除的是bumb ...