1105. Spiral Matrix (25)
This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasing order. A spiral matrix is filled in from the first element at the upper-left corner, then move in a clockwise spiral. The matrix has m rows and n columns, where m and n satisfy the following: m*n must be equal to N; m>=n; and m-n is the minimum of all the possible values.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N. Then the next line contains N positive integers to be filled into the spiral matrix. All the numbers are no more than 104. The numbers in a line are separated by spaces.
Output Specification:
For each test case, output the resulting matrix in m lines, each contains n numbers. There must be exactly 1 space between two adjacent numbers, and no extra space at the end of each line.
Sample Input:
12
37 76 20 98 76 42 53 95 60 81 58 93
Sample Output:
98 95 93
42 37 81
53 20 76
58 60 76
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<math.h>
using namespace std;
int ans[][]; bool cmp(int a,int b)
{
return a > b;
}
int main()
{
int len,tem;
scanf("%d",&len);
vector<int> vv;
for(int i = ;i <= len ;++i)
{
scanf("%d",&tem);
vv.push_back(tem);
}
sort(vv.begin(),vv.end(),cmp);
int n,m;
n = sqrt((double)len);
while(len % n != )
{
--n;
}
m = len / n;
int high = , low = m, left = ,right = n;
int x = ,y = ;
for(int i = ;i < len ;++i)
{
while(i < len && x <= right)
{
ans[y][x] = vv[i];
++i;
++x;
}
--x;
++y;
++high;
while(i < len && y <= low)
{
ans[y][x] = vv[i];
++i;
++y;
}
--y;
--x;
--right;
while(i < len && x >= left)
{
ans[y][x] = vv[i];
++i;
--x;
}
++x;
--y;
--low;
while(i < len && y >= high)
{
ans[y][x] = vv[i];
++i;
--y;
}
++y;
++x;
++left;
--i;
}
for(int i = ;i <= m ;++i)
{
for(int k = ;k <= n ;++k)
{
if(k == ) printf("%d",ans[i][k]);
else printf(" %d",ans[i][k]);
}
printf("\n");
}
return ;
}
1105. Spiral Matrix (25)的更多相关文章
- 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) 1105. Spiral Matrix (25)
简单模拟. #include<cstdio> #include<cstring> #include<cmath> #include<map> #incl ...
- 【PAT甲级】1105 Spiral Matrix (25分)
题意:输入一个正整数N(实则<=1e5),接着输入一行N个正整数(<=1e4).降序输出螺旋矩阵. trick: 测试点1,3运行超时原因:直接用sqrt(N)来表示矩阵的宽会在N是素数时 ...
- PAT甲级——1105 Spiral Matrix (螺旋矩阵)
此文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90484058 1105 Spiral Matrix (25 分) ...
- PAT 1105 Spiral Matrix[模拟][螺旋矩阵][难]
1105 Spiral Matrix(25 分) This time your job is to fill a sequence of N positive integers into a spir ...
- 1105 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
This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasi ...
- PAT 甲级 1105 Spiral Matrix
https://pintia.cn/problem-sets/994805342720868352/problems/994805363117768704 This time your job is ...
- PAT 1105 Spiral Matrix
This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasi ...
随机推荐
- iOS - UI - UISlider
6.UISlider //滑块 设置高度 UISlider * slider = [[UISlider alloc] initWithFrame:CGRectMake(20, 100, CGRec ...
- (转)JavaScript 中对变量和函数声明的“提前(hoist)”
变量声明“被提前” JavaScript 的语法和 C .Java.C# 类似,统称为 C 类语法.有过 C 或 Java 编程经验的同学应该对“先声明.后使用”的规则很熟悉,如果使用未经声明的变量或 ...
- .Net (MVC) 随机生成验证码
以前一直对C#的GDI画图部分知识点不怎么用所以忘得差不多了,这两天正好公司要做一个博客系统,其中一个需求就是留言时为了防止恶意攻击必须填写验证码,正好借着这个机会复习了一下,以下是实现代码,写的比较 ...
- Linux之apt-get无sudo权限安装软件
apt-get source package cd package ./configure --prefix=$HOME make make install
- linux /etc/sysctl.conf 禁止别人ping自己
vi /etc/sysctl.conf如果希望屏蔽别人 ping 你的主机,则加入以下代码:# Disable ping requestsnet.ipv4.icmp_echo_ignore_all = ...
- 浮出层的css写法,完美兼容IE6~10
利用元素间的绝对定位差一像素,使用不同颜色做出浮出层小三角的效果,完美兼容各浏览器! html部分: <div class="poptip"> <span cla ...
- (转)Android面试题
1. 下列哪些语句关于内存回收的说明是正确的? (b ) A. 程序员必须创建一个线程来释放内存 B.内存回收程序负责释放无用内存 C.内存回收程序允许程序员直接释放内存 D.内存回收程序可以在 ...
- JavaScript之返回顶部
为了弄这个Hexo,今天又是坐在电脑面前待了一天( ⊙ o ⊙ ),老是出问题,在百度上也试验了很多方法,还是没弄好,诶...身心疲惫甚是乏累啊~~~ 算了,这个Hexo先不弄,还是安分点吧,在Hex ...
- JavaScript之表格过滤器
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Oracle 联合主键
alter table NCJSYD add constraints NCJSYD_pk primary key (YR,DQ);