【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 ...
随机推荐
- 自定义Nginx返回页面
1.403返回页面 #user nobody; worker_processes ; #error_log logs/error.log; #error_log logs/error.log noti ...
- vm virtualbox 里的ubuntu挂载windows的文件夹
1.先在virtualbox里设置好共享文件夹 如右图所示: 共享文件夹-添加共享文件夹 里面: 选择好 共享文件夹路径,起好 共享文件夹名称,下面都不用选. 假设共享文件名是ShareD, 2. 然 ...
- 0216 aop和打印数据库执行日志
需求 maven依赖 <dependency> <groupId>p6spy</groupId> <artifactId>p6spy</artif ...
- JS高级---识别正则表达式是否匹配
识别正则表达式是否匹配 console.log(/[a-zA-Z]+/.test("hello")); console.log(/./.test("除了回车换行以为的任意 ...
- 全文检索工具elasticsearch和kibana安装
一.安装elasticsearch 1.拷贝elasticsearch-5.6.4.rpm到/opt目录下「cenos7」 systemctl list-unit-files|grep elastic ...
- git回退版本: 回退本地代码版本 + 回退服务器代码版本
1.回退本地代码版本 借助IDEA开发工具回退版本,点击Version Control ,查看历史版本号: 右击想要回退的版本号,选择Reset Current Branch hear... 选择 H ...
- winform常用控件介绍
1.窗体 12.Label 控件 33.TextBox 控件 44.RichTextBox控件 55.NumericUpDown 控件 76.Button 控件 77.GroupBox 控件 78.R ...
- mysql 中LIKE 与FIND_IN_SET 与关联表left join 速度效率比较
有一张表Table有IDStr字段,如下只显示二个字段还有很多其他字段 方式一 字段逗号分割,直接用UserIDStr字段,里面存多个ID用逗号分割 UUID UserIDStr 1111 1,2,3 ...
- SSH多表操作入门
这个系统写到这里,所涉及到的都是单表的操作,增删改查,现在功能需要完善,涉及到多表操作,开始是毫无头绪,书上的代码也没有现成的可以借鉴,想来就从最简单的开始.问题出现了很多,不过最后在龙哥的提示下还是 ...
- ubuntu 安装谷歌
下载ubuntugoogle官网:http://www.ubuntuchrome.com/ 我的是ubuntuLTS稳定版: 上传Ubuntu: 执行解压操作: sudo dpkg -i goo ...