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. PyQt5 安装及简单实例 -- 标签设置

    - 1 -    PyQt5安装,鉴于pip工具,安装其实很简单, 如下:(最好将pip索引配置成国内镜像,速度比默认的快得多) pip3 isntall PyQt5 - 2 -    利用Pycha ...

  2. Java中逗号运算符的使用

    今天看到一道面试题,题目如下: 题目解释: 上面有一个类,有四个成员属性,以及两个构造方法(一个四个参数,一个两个参数),题目给出四个选项,问哪些选项可以满足在在四个参数的构造方法中完成x=a,y=b ...

  3. 深度探索C++对象模型之第一章:关于对象之C++对象模型

    一.C和C++对比: C语言的Point3d: 数据成员定义在结构体之内,存在一组各个以功能为导向的函数中,共同处理外部的数据. typedef struct point3d { float x; f ...

  4. CSIC_716_20191116【常用模块的用法 time ,datetime, random, os, sys, hashlib】

    import time import datetime import os import sys import random import hashlib time模块 时间戳(Timestamp) ...

  5. webstorm安装与破解

    1.下载webstorm和补丁文件 链接:https://pan.baidu.com/s/1aiHxPExAbDCcHxKtB82_vg 提取码:jo07 链接:https://pan.baidu.c ...

  6. jQuery实现的文字逐行向上间歇滚动效果示例

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  7. mysql主从同步的键值冲突问题的解决方法

    转自https://njs375666635.iteye.com/blog/2242067 多主互备和主从复制有一些区别,因为多主中都可以对服务器有写权限,所以设计到自增长重复问题 出现的问题(多主自 ...

  8. 校园商铺-4店铺注册功能模块-8店铺注册之Controller层的改造

    不合理的地方: 1. 并不需要将InputStream转换成File类型,直接将InputStream传进入交给CommonsMultipartfile去处理就可以了 如果做这样的转换,每次都需要生成 ...

  9. python处理多线程之间事件通讯方法

    一.什么是事件 每执行一个事情,肯定有该事情的执行后状态,那事件就是该事情发生的信号 在程序中,多线程之间需要通讯,而事件就是方便线程之间的通讯 案例: 1.服务器启动需要5秒 2.客服端启动后去链接 ...

  10. nginx css,js合并插件,淘宝nginx合并js,css插件

    先下载Nginx_concat_module,下载后把它放在/usr/local/src/文件夹中,新建文件夹nginx-http-concat把下载的config  ngx_http_concat_ ...