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. python接口自动化(get请求)

    python接口自动化(get请求) get请求的目的:查询资源 一.导包 二.请求的URL 三.请求的参数 四.获取请求的URL 五.获取响应的状态码 六.获取响应的本文信息 #导包 import ...

  2. Linux(Centos7)安装ngnix服务器

    Ngnix服务器是一款优秀的静态页服务器软件和反向代理服务器软件 目前,centos安装ngnix可以yum安装也可以下载安装,我们为了扩展方便,选择下载安装.yum一键安装没什么好说的. 一.安装编 ...

  3. sql 循环执行游标

    ---定义开始和结束时间 declare @st_dt datetime declare @en_dt datetime ---时间赋值 ' ' ---定义中间变量 declare @dt datet ...

  4. PROJECT | 四则运算UI设计 - PSP表格&需求分析

    PSP表格(TP版) 需求分析 [GUI编程语言选择] 考虑到Java编写GUI效率偏低且界面不算特别美观(即使有Windowbuilder插件帮助),所以我们使用控件更多,开发效率更高,具有集成开发 ...

  5. 跟我一起使用Vue.js + Docker 部署项目

    本文学习自:https://juejin.im/post/5bee5ddde51d457b8a33938c 项目环境是在ubuntu下,记得要在root目录下,不然安装vue会报错 npm insta ...

  6. 安装Epson打印机但因lsb依赖错误而中断的驱动程序

    sudo apt-get install printer-driver-escpr 安装Epson打印机但因lsb依赖错误而中断的驱动程序 问题: 我正在安装 Epson XP-310 驱动程序,从这 ...

  7. not registered via @EnableConfigurationProperties or marked as Spring component

    利用@ConfigurationProperties(prefix = "")来绑定属性时报错: not registered via @EnableConfigurationPr ...

  8. MD5 AND JSON AND XML

    MD5JSON.h #pragma once #include "include/json/json.h" #include "include/md5/md5.h&quo ...

  9. python封装email模块

    一.代码 from email.mime.text import MIMEText from email.header import Header from email.utils import pa ...

  10. Java反射简介

    Java反射简介 1.Class类 1) 在面向对象的世界里,万事万物皆对象.(java语言中,静态的成员.普通数据类型除外) 类是不是对象呢?类是(哪个类的对象呢?)谁的对象呢? 类是对象,类是ja ...