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 10​4​​. 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:


Sample Output:

  1. 知识点:简单模拟;vector的使用
    思路:
    矩阵应该利用vector的数组来构建;因为如果是形成比较方正的矩阵,长和宽最大是100左右;但如果是质数,矩阵就会变成长条形长在10000以内,这样利用普通二维数组数组会超限;利用了vector容器可以.resize()的特点。
    单列的输出特殊处理
  2.  
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <vector>
  6. using namespace std;
  7. const int maxn = ;
  8. vector<int> matrix[maxn];
  9. bool cmp(int a,int b){
  10. return a>b;
  11. }
  12. int getLong(int n){
  13. int i;
  14. for(i=sqrt(n)+0.9;i<=n && n%i!=;i++);
  15. return i;
  16. }
  17. int main(){
  18. vector<int> list;
  19. int n,tmp; cin >> n;
  20. for(int i=;i<n;i++){
  21. scanf("%d",&tmp);
  22. list.push_back(tmp);
  23. }
  24. sort(list.begin(), list.end(), cmp);
  25. int l = getLong(n);
  26. int w = n/l;
  27. for(int i=;i<=l;i++){
  28. matrix[i].resize(w+);
  29. }
  30. int x=, y=, ptr=;
  31. int base=;
  32. while(ptr<list.size()){
  33. if(w==){
  34. for(;y<=l+base;y++){
  35. matrix[y][x]=list[ptr++];
  36. }
  37. break;
  38. }
  39. for(; x<=w+base; x++){
  40. matrix[y][x]=list[ptr++];
  41. }
  42. x--; y++;
  43. for(; y<l+base; y++){
  44. matrix[y][x]=list[ptr++];
  45. }
  46. for(; x>=+base; x--){
  47. matrix[y][x]=list[ptr++];
  48. }
  49. x++; y--;
  50. for(; y>+base; y--){
  51. matrix[y][x]=list[ptr++];
  52. }
  53. x++; y++;
  54. w-=; l-=;
  55. base++;
  56. //printf("%d %d w=%d l=%d",x,y,w,l);
  57. }
  58. for(int i=;i<=getLong(n);i++){
  59. for(int j=;j<=(n/getLong(n));j++){
  60. if(j!=) printf(" ");
  61. printf("%d",matrix[i][j]);
  62. }
  63. printf("\n");
  64. }
  65. }

1105 Spiral Matrix的更多相关文章

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

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

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

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

  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. PAT 甲级 1105 Spiral Matrix

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

  5. 1105 Spiral Matrix(25 分)

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

  6. PAT 1105 Spiral Matrix

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

  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甲级】1105 Spiral Matrix (25分)

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

随机推荐

  1. WebAPI支持Session

    1.在App_Start/WebApiConfig.cs中建立建立HttpControllerHandler和HttpControllerRouteHandler 并覆写它: public class ...

  2. vue2.0插件

    1.better-scroll 参考网址:https://ustbhuangyi.github.io/better-scroll/doc/zh-hans/ better-scroll 是什么 firs ...

  3. 操作系统的发展史 day36

    什么是操作系统       可能很多人都会说,我们平时装的windows7 windows10都是操作系统,没错,他们都是操作系统.还有没有其他的? 想想我们使用的手机,Google公司的Androi ...

  4. 1、javaweb学习之配置文件web.xml

    今天这里主要讲述javaweb中的配置文件web.xml中的内容及其作用,都是基础部分,对于初学者需要好好掌握理解. 简单配置: <servlet>    <servlet-name ...

  5. Linux CentOS 7 下 Apache Tomcat 7 安装与配置

    前言 记录一下Linux CentOS 7安装Tomcat7的完整步骤. 下载 首先需要下载tomcat7的安装文件,地址如下: http://mirror.bit.edu.cn/apache/tom ...

  6. hdu 4004 (二分加贪心) 青蛙过河

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4004 题目意思是青蛙要过河,现在给你河的宽度,河中石头的个数(青蛙要从石头上跳过河,这些石头都是在垂 ...

  7. 三种简单排序算法(java实现)

    一.冒泡排序 算法思想:遍历待排序的数组,每次遍历比较相邻的两个元素,如果他们的排列顺序错误就交换他们的位置,经过一趟排序后,最大的元素会浮置数组的末端.重复操                   作 ...

  8. MySQL优化(一) 优化关键技术

    MySql的优化是一个综合性的技术,主要包括有: (1)表的设计合理化(符合 3NF 三范式) (2)添加适当的索引(Index):索引分类:普通索引.主键索引.唯一索引.全文索引(文本).空间索引. ...

  9. 关于RNA-Seq数据去接头(Adapter)这事需要讲一讲

    关于RNA-Seq数据去接头(Adapter)这事需要讲一讲 RNA-Seq adapter barcode cutadapt 首先来了解一下三个概念: 1.adapter是一段短的序列已知的核酸链, ...

  10. mysql 压缩方法

    show global variables like 'innodb_file_format%';alter table t row_format=COMPRESSED;