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. android 中对于采用okhttp时获取cookie并放入webview实现跳过登陆显示页面的功能

    最近项目需要将网页的一些信息展示到app当中,由于采用的是okhttp进行网络的访问,并采用了cookie对于每次的访问请求都做了验证,所以在加入webview显示网页的时候会需要进行一下验证,为了跳 ...

  2. 【python调用windows CLI】调用adb统计Android app的流量消耗

    主要记录python如何调用windows CLI 手机连接PC,adb devices可以看到手机sn 通过adb 获取指定app的processID UID 读取Android  /proc/ne ...

  3. Gradle实战:不同编译类型的包同设备共存

    查看原文:http://blog.csdn.net/u010818425/article/details/52335844 Gradle实战系列文章: <Gradle基本知识点与常用配置> ...

  4. codeforces 590A A. Median Smoothing(思维)

    题目链接: A. Median Smoothing time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  5. Pascal 语言中字符与字符串

    [题目]输入一段文章(255个字符以内),求文章中单词的个数,相同单词只记一次,The 和 the 视作相同. [敲代码] //网友代码 var article,w:string; arr:array ...

  6. 转: 数字证书原理 https 完整过程解析

    点评: 讲的非常的详细与全面,值得一看. 转: http://www.cnblogs.com/JeffreySun/archive/2010/06/24/1627247.html 文中首先解释了加密解 ...

  7. 一个Itextsharp 批量添加图片到pdf 方法

    这里我就直接把我的页面贴进来了 using System; using System.Collections.Generic; using System.Web; using System.Web.U ...

  8. OpenShare:前所未有的开放性

    客户总是面临一个选择:开放的企业门户产品 vs 封闭的企业门户产品 市场上大多数企业门户产品是自成一体的其实也就是封闭的,他们不能和企业目录集成,不能和Exchange集成,不能和SAP集成,不能和L ...

  9. part 2 Angular modules and controllers

    What is a module in AngularJS? A module is a container for different parts of your application i.e c ...

  10. 百度网页搜索部来自Console的招聘信息

    百度网页搜索部来自Console的招聘信息,小伙伴们,你发现了吗?