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 Npositive integers to be filled into the spiral matrix. All the numbers are no more than 1. 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 <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
int nn, m, n;
int main()
{
cin >> nn;
vector<int>v(nn);
for (int i = ; i < nn; ++i)
cin >> v[i];
n = floor(sqrt(nn));//取小值
while (nn%n!=)n--;//找到m,n
m = nn / n;
vector<vector<int>>arry(m, vector<int>(n, ));
sort(v.begin(), v.end(), [](int a, int b) {return a > b; });
int lm = , ln = ;//左上角
int rm = m - , rn = n - ;//右下角
int k = ;//使用数据的下角标
while (lm <= rm && ln <= rn && k < nn)
{
if (lm == rm)//只有一行,则打印
for (int i = ln; i <= rn; ++i)
arry[lm][i] = v[k++];
else if (ln == rn)//只有一列
for (int i = lm; i <= rm; ++i)
arry[i][ln] = v[k++];
else
{
for (int i = ln; i < rn; ++i)//上行
arry[lm][i] = v[k++];
for(int i=lm;i<rm;++i)//右列
arry[i][rn]= v[k++];
for (int i = rn; i > ln; --i)//下行
arry[rm][i] = v[k++];
for (int i = rm; i > lm; --i)
arry[i][ln] = v[k++];
}
lm++, ln++;//左上角右下移
rm--, rn--;//右下角左上移
}
for (int i = ; i < m; ++i)
{
for (int j = ; j < n; ++j)
cout << arry[i][j] << (j == n - ? "" : " ");
cout << endl;
}
return ;
}

PAT甲级——A1105 Spiral Matrix【25】的更多相关文章

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

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

  2. PAT 甲级 1105 Spiral Matrix

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

  3. 1105. Spiral Matrix (25)

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

  4. A1105. Spiral Matrix

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

  5. 【PAT甲级】1070 Mooncake (25 分)(贪心水中水)

    题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全 ...

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

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

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

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

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

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

  9. PAT甲级 1122. Hamiltonian Cycle (25)

    1122. Hamiltonian Cycle (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...

随机推荐

  1. java获得磁盘、网络实时I/O速率

    最近项目中需要一个平台硬件资源的监控模块,当时采用了Sigar中api,但是做到针对磁盘和网络的实时I/O速率的时候发现Sigar并没有直接支持的接口.于是……它就诞生了.底层采用C++编写,通过ja ...

  2. iOS进阶一OC对象的本质

    OC对象的本质 平时编写的Object-C代码,底层实现其实都是C/C++代码. 所以Objective-C的面向对象都是基于C/C++的数据结构实现的,OC对象内部可以容纳不同数据类型的数据,因此可 ...

  3. shell 命令 进程相关

     1. 进程标识号PID 唯一性 pid 为0    内核进程,linux内核创建 pid 为1    init进程,系统最早创建的进程,init是所有用户进程的祖先 2. 查看系统进程信息 (1)[ ...

  4. gerp 用法

    1. 提取/etc/ansible/hosts 文件中包括star的行,且包含数字,不以[为开头的行 grep 'star' /etc/ansible/hosts | grep  '[[:digit: ...

  5. 幂等 zuul的Filter实现

    通过zuul的过滤器 filter实现 //app 幂等过滤 @SuppressWarnings("all") @Order(Ordered.HIGHEST_PRECEDENCE) ...

  6. Go, JS和Websocket

    JS中建立Websocket连接 var ws = new WebSocket("ws://hostname/path", ["protocol1", &quo ...

  7. sikuli+eclipse对于安卓app自动化测试的应用(第一次写博客,有些语言还不太专业,望海涵)

    Sikuli是什么? 下面是来自于官网的介绍:Sikuli is a visual technology to automate and test graphical user interfaces ...

  8. 阿里云ECS发送邮件失败

    阿里云发送SMTP邮件失败   N多测试发现 阿里云服务器不能用25端口发邮件,配置465端口阿里云发送邮件是成功的 修改mail.rc 里的smtp 端口为465 配置如下 set from=*** ...

  9. Map按键排序(sort by key)

    1.需求:已知有如下map,要求按照key倒序排列遍历. Map<String, Integer> map = new HashMap<>(); map.put("1 ...

  10. 利用redis的bitmap实现用户签到功能

    一.场景需求 适用场景如签到送积分.签到领取奖励等,大致需求如下: 比如签到1天送1积分,连续签到2天送2积分,3天送3积分,3天以上均送3积分等. 如果连续签到中断,则重置计数,每月初重置计数. 显 ...