PAT (Advanced Level) 1105. Spiral Matrix (25)
简单模拟。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<queue>
#include<string>
#include<algorithm>
using namespace std; const int maxn=+;
int N,a[maxn];
int ans[maxn];
int m,n;
int dir[][]; void f()
{
dir[][]=;dir[][]=;
dir[][]=;dir[][]=;
dir[][]=;dir[][]=-;
dir[][]=-;dir[][]=;
} bool cmp(const int&a,const int&b)
{
return a>b;
} int main()
{
scanf("%d",&N);
f();
for(int i=;i<=N;i++) scanf("%d",&a[i]);
sort(a+,a++N,cmp); for(int i=sqrt(1.0*N);i>=;i--)
if(N%i==){n=i,m=N/i;break;} memset(ans,-,sizeof ans);
for(int i=;i<=m;i++)
for(int j=;j<=n;j++)
ans[(i-)*n+j]=; int nowDir=;
int nowX=,nowY=;
for(int i=;i<=N;i++)
{
ans[(nowX-)*n+nowY]=a[i];
int NewX=nowX+dir[nowDir][];
int NewY=nowY+dir[nowDir][];
if(ans[(NewX-)*n+NewY]!=||NewX<=||NewX>=m+||NewY<=||NewY>=n+) nowDir=(nowDir+)%;
nowX=nowX+dir[nowDir][];
nowY=nowY+dir[nowDir][];
}
int num=;
// for(int i=1;i<=m*n;i++) printf("%d ",ans[i]); for(int i=;i<=m;i++)
for(int j=;j<=n;j++)
{
printf("%d",ans[num++]);
if(j<n) printf(" ");
else printf("\n");
}
return ;
}
PAT (Advanced Level) 1105. Spiral Matrix (25)的更多相关文章
- 【PAT甲级】1105 Spiral Matrix (25分)
题意:输入一个正整数N(实则<=1e5),接着输入一行N个正整数(<=1e4).降序输出螺旋矩阵. trick: 测试点1,3运行超时原因:直接用sqrt(N)来表示矩阵的宽会在N是素数时 ...
- 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甲题题解-1105. Spiral Matrix (25)-(模拟顺时针矩阵)
题意:给定N,以及N个数.找出满足m*n=N且m>=n且m-n最小的m.n值,建立大小为m*n矩阵,将N个数从大到下顺时针填入矩阵中. #include <iostream> #in ...
- PAT (Advanced Level) 1114. Family Property (25)
简单DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...
- PAT (Advanced Level) 1109. Group Photo (25)
简单模拟. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...
- PAT (Advanced Level) 1101. Quick Sort (25)
树状数组+离散化 #include<cstdio> #include<cstring> #include<cmath> #include<map> #i ...
- PAT (Advanced Level) 1071. Speech Patterns (25)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- PAT (Advanced Level) 1063. Set Similarity (25)
读入之后先排序. 询问的时候可以o(m)效率得到答案. #include<cstdio> #include<cstring> #include<cmath> #in ...
- PAT (Advanced Level) 1059. Prime Factors (25)
素因子分解. #include<iostream> #include<cstring> #include<cmath> #include<algorithm& ...
随机推荐
- Linux下gcc编译生成动态链接库*.so文件并调用它【转载】
动态库*.so在linux下用c和c++编程时经常会碰到,最近在网站找了几篇文章介绍动态库的编译和链接,总算搞懂了这个之前一直不太了解得东东,这里做个笔记,也为其它正为动态库链接库而苦恼的兄弟们提供一 ...
- sipcal
sudo apt-get install sipcalc sipcalc 10.120.148.xx/20
- WPF类层次结构
WPF类层次结构 System.Threading.DispatcherObject类 WPF应用程序使用STA(Single Thread Affinity)模型,整个用户界面由一个单独的线程拥有, ...
- angular项目——小小记事本2
一,路由的规划. 需要模拟的页面有三个:all,active,conplete. 首先,写好铺垫需要的各种东西,重要的组件的引用等—— 这里我们会将index.html设为主页,将body.html加 ...
- Python编码介绍——encode和decode
在 python 源代码文件中,如果你有用到非ASCII字符,则需要在文件头部进行字符编码的声明,声明如下: # code: UTF-8 因为python 只检查 #.coding 和编码字符串,所以 ...
- mvn 下载源码和javadoc
1. 下载源码,两种: . mvn dependency:sources . mvn eclipse:eclipse -DdownloadSources=true 2. 下载javadoc,两种: . ...
- hdu_2224_The shortest path(dp)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2224 题意:双调欧几里德旅行商经典问题,找一条最短回路使得该路经过所有的点 题解:dp[i][j]=d ...
- LeetCode OJ 110. Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- mysql中出现没有权限访问或者查看全部数据库的问题
在my.cnf中mysqld目录下的 socket=/var/lib/mysql/mysql.sock一行下面添加 skip_grant_tables---------------报错退出 serv ...
- viewpager处理(二):设置当前item时,让viewpager不出现滑动动画
我们在使用viewpager.setCurrentItem(position);的时候,viewpager出现滑动动画,如何去掉这个动画呢? 可以用viewpager.setCurrentItem(p ...