题意:
输入一个正整数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. const真有点烦

    在C++中const代替#define的操作,当定义一个const时必须附一个值给它:const int size = 100;通常C++编译器不为const创建存储空间,相反它把这个定义保存在符号表 ...

  2. 164.扩展User模型-继承AbstractUser

    继承自AbstractUser: 如果Abstractuser中定义的字段不能够满足你的项目的要求,并且不想要修改原来User对象上的一些字段,只是想要增加一些字段,那么这时候可以直接继承自djang ...

  3. Jmeter-基础实战

    一.测试需求:测试20个用户访问web网站在负载达到30QPS时的平均响应时间 QPS:Query Per Second 每秒查询率.是一台查询服务器每秒能够处理的查询次数.在因特网上,作为域名系统服 ...

  4. HTML的学习结构

    HTML的学习结构 HTML的背景 HTML的创建 HTML的网页基本结构 HTML的基本标签 HTML的图像标签 HTML的链接标签 HTML的列表标签和表格标签 HTML的媒体元素(视频+音频) ...

  5. socket编程(python)

    交互原理: 服务端和客户端通过底层socket接口编程通信,交互的信息都是通过byte字节形式传递,网络传输中不能保证信息完整传输有可能是分片传输,所以可能从缓冲区获取的信息需要分段拼接或拆分组合成一 ...

  6. PP:Classification of Time-Series Images Using Deep Convolutional Neural Networks

    The 10th international conference on machine vision; C类 Methodology: 非主流方法 2 stages: 1. convert time ...

  7. python pymysql 基本使用

    from pymysql import * # 1.创建连接数据库 conn = connect(host="localhost", port=3306, user="r ...

  8. Window逆向基础之逆向工程介绍

    逆向工程 以设计方法学为指导,以现代设计理论.方法.技术为基础,运用各种专业人员的工程设计经验.知识和创新思维,对已有产品进行解剖.深化和再创造. 逆向工程不仅仅在计算机行业.各行各业都存在逆向工程. ...

  9. codeforces 1282C. Petya and Exam (贪心)

    链接:https://codeforces.com/contest/1282/problem/C 题意:  有一个人参加考试,考试只有两种题,一种是简单题,每道题耗时固定为a:另一种是困难题,每道题耗 ...

  10. Git 时光穿梭机

    git log 提交日志 git reflog 命令日志 git status 查看状态 管理修改 git diff 工作区与暂存区 git diff master 工作区与版本库 git diff ...