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 ...
随机推荐
- Linux下MySQL主从同步配置
Centos6.5 MySQL主从同步 MySQL版本5.6.25 主服务器:centos6.5 IP:192.168.1.101 从服务器:centos6.5 IP:192.168.1.102 一. ...
- java执行程序
执行jar java -jar x.jar 执行jar里边指定class java -cp x.jar x.x.x
- poj动态规划列表
[1]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 13 ...
- [wordpress] determine_current_user 在get_current_user_id() 或者 wp_get_current_user()会调用
在看了Cookie Authentication In A AngularJS WordPress Theme之后,清楚了当Wordpress在中使用get_current_user_id() 或者w ...
- 纯css3 transforms 3D文字翻开翻转3D开放式效果
详细内容请点击 在线预览立即下载 在本教程中,将基于CSS3创建的一个实现一个有趣的3D开放式效果.教程的目的是展示我们如何能带来一些生活上使用CSS3 . html: <ul class=&q ...
- 获取动态SQL的返回结果
1. 介绍说明 有时候在执行存储过程后,需要获取存储过程返回的列表,然后进行相应操作的情况,或者执行动态语句,获取返回结果的情况,通过EXEC ,sp_executesql可以实现该功能. 网上也有很 ...
- android四种更新UI的方法
笔记: // 使用handler.post(Runnable)更新UI public void updateUI_Fun1() { new Thread() { public void run() { ...
- 物联网 WIFI 一键配置原理(smartconfig) ESP8266/QCA4004
自从物联网 问世以来,如何使得物 能够联网 有了很多的方式,目前运用非常广的WIFI,今天就总结下自这个方面,也对于有需要的盆友 也希望有抛砖引玉之效果. 物联网: 智能硬件+APP+云 APP ...
- JavaScript之放大镜效果2
在放大图片效果的同时,我们怎么原图和放大窗体增加间隔呢? 我们只需应用一个table就行了: 源码上: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML ...
- PictureBox控件鼠标进入的手形改变和提示
PictureBox控件载入了图片后,如果要设置其为链接作用的功能,一般需要当鼠标移动到其上时鼠标自动变为手形以提示用户此时可以点击,如果图形无法方便辨识链接的具体功能,最好此时给与一定提示. 1.鼠 ...