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)的更多相关文章

  1. PAT甲题题解-1105. Spiral Matrix (25)-(模拟顺时针矩阵)

    题意:给定N,以及N个数.找出满足m*n=N且m>=n且m-n最小的m.n值,建立大小为m*n矩阵,将N个数从大到下顺时针填入矩阵中. #include <iostream> #in ...

  2. PAT (Advanced Level) 1105. Spiral Matrix (25)

    简单模拟. #include<cstdio> #include<cstring> #include<cmath> #include<map> #incl ...

  3. 【PAT甲级】1105 Spiral Matrix (25分)

    题意:输入一个正整数N(实则<=1e5),接着输入一行N个正整数(<=1e4).降序输出螺旋矩阵. trick: 测试点1,3运行超时原因:直接用sqrt(N)来表示矩阵的宽会在N是素数时 ...

  4. PAT甲级——1105 Spiral Matrix (螺旋矩阵)

    此文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90484058 1105 Spiral Matrix (25 分) ...

  5. PAT 1105 Spiral Matrix[模拟][螺旋矩阵][难]

    1105 Spiral Matrix(25 分) This time your job is to fill a sequence of N positive integers into a spir ...

  6. 1105 Spiral Matrix(25 分)

    This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasi ...

  7. 1105 Spiral Matrix

    This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasi ...

  8. PAT 甲级 1105 Spiral Matrix

    https://pintia.cn/problem-sets/994805342720868352/problems/994805363117768704 This time your job is ...

  9. PAT 1105 Spiral Matrix

    This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasi ...

随机推荐

  1. c++ 的几种强制转换的讨论

    http://www.cnblogs.com/jerry19880126/archive/2012/08/14/2638192.html static_cast  : 同 c 的强制转换类似: rei ...

  2. C#调用C++的DLL搜集整理的所有数据类型转换方式

    //C#调用C++的DLL搜集整理的所有数据类型转换方式,可能会有重复或者多种方案,自己多测试 //c++:HANDLE(void *) ---- c#:System.IntPtr //c++:Byt ...

  3. Java中创建操作文件和文件夹的工具类

    Java中创建操作文件和文件夹的工具类 FileUtils.java import java.io.BufferedInputStream; import java.io.BufferedOutput ...

  4. codeforces 434A A. Ryouko's Memory Note(数学)

    题目链接: A. Ryouko's Memory Note time limit per test 1 second memory limit per test 256 megabytes input ...

  5. Python中的内置函数

    2.1 Built-in Functions The Python interpreter has a number of functions built into it that are alway ...

  6. Android简易注解View(java反射实现)

    一.引言 Android中通过findViewById在布局文件中找到需要的View,加入一个Activity里面有许多的View需要初始化,那将是一件很繁琐的事情.当然Google一下你会发现有很多 ...

  7. 【CSS3】---层模型position之fixed固定定位、absolute绝对定位和relative相对定位

    什么是层模型? 什么是层布局模型?层布局模型就像是图像软件PhotoShop中非常流行的图层编辑功能一样,每个图层能够精确定位操作,但在网页设计领域,由于网页大小的活动性,层布局没能受到热捧.但是在网 ...

  8. Java开发从零开始填坑

    开始学习Java,感觉较.NET知识更零碎一些,所以开个帖子把自己踩过的坑记录下来,都是边边角角网上不容易找到的东西. 1.java命令格式:>cd %parent-of-pakadgePath ...

  9. Cannot find protocol declaration for "XXDelegate" 找不到协议错误

    原因是 在A里面继承了B类里面的"XXDelegate",在B类的头文件里又导入了A类的头文件 解决方法 不在B类的头文件导入A类的头文件,改成在B类的.m文件导入A类的头文件

  10. 读书笔记-UIView与控件

    1.UIView 在Objective-C中,NSObject是所有类的“根”类.同样,在UIKit框架中,也存在一个如此神奇的类UIView.从继承关系上看,UIView是所有视图的根. 1.1.U ...