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:

  

  
知识点:简单模拟;vector的使用
思路:
矩阵应该利用vector的数组来构建;因为如果是形成比较方正的矩阵,长和宽最大是100左右;但如果是质数,矩阵就会变成长条形长在10000以内,这样利用普通二维数组数组会超限;利用了vector容器可以.resize()的特点。
单列的输出特殊处理
 #include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
const int maxn = ; vector<int> matrix[maxn]; bool cmp(int a,int b){
return a>b;
} int getLong(int n){
int i;
for(i=sqrt(n)+0.9;i<=n && n%i!=;i++);
return i;
} int main(){
vector<int> list;
int n,tmp; cin >> n;
for(int i=;i<n;i++){
scanf("%d",&tmp);
list.push_back(tmp);
}
sort(list.begin(), list.end(), cmp);
int l = getLong(n);
int w = n/l;
for(int i=;i<=l;i++){
matrix[i].resize(w+);
}
int x=, y=, ptr=; int base=;
while(ptr<list.size()){
if(w==){
for(;y<=l+base;y++){
matrix[y][x]=list[ptr++];
}
break;
}
for(; x<=w+base; x++){ matrix[y][x]=list[ptr++];
} x--; y++;
for(; y<l+base; y++){
matrix[y][x]=list[ptr++];
}
for(; x>=+base; x--){
matrix[y][x]=list[ptr++];
}
x++; y--;
for(; y>+base; y--){
matrix[y][x]=list[ptr++];
}
x++; y++;
w-=; l-=;
base++;
//printf("%d %d w=%d l=%d",x,y,w,l); }
for(int i=;i<=getLong(n);i++){
for(int j=;j<=(n/getLong(n));j++){
if(j!=) printf(" ");
printf("%d",matrix[i][j]);
}
printf("\n");
}
}

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. sqlserver打开对象资源管理器管理的帮助文档的快捷键

    sqlserver打开对象资源管理器管理的帮助文档的快捷键:Fn+F1

  2. Dom,pull,Sax解析XML

    本篇随笔将详细讲解如何在Android当中解析服务器端传过来的XML数据,这里将会介绍解析xml数据格式的三种方式,分别是DOM.SAX以及PULL. 一.DOM解析XML 我们首先来看看DOM(Do ...

  3. 案例情景--在一次Oracle 数据库导出时 EXP-00008;ORA-00904:EXP-00000: oracle不同版本导入导出规则

    案例情景--在一次Oracle 数据库导出时: C:\Documents and Settings\Administrator>exp lsxy/lsxy@lsxy_db file=E:\lsx ...

  4. python下的MySQL数据库编程

    https://www.tutorialspoint.com/python/python_database_access.htm if you need to access an Oracle dat ...

  5. 20172306《Java程序设计》第四周学习总结

    20172306 <Java程序设计>第四周学习总结 教材学习内容总结 第四章: 1. 类和对象的回顾:除了看书,我还上网找了一下两者的一些区别. 2. 编写类时,了解到初始化.形式参数. ...

  6. day 3:注释,缩进

    本节内容: 1,注释 2,缩进 1,注释: 使用#可以注释单行 # print("hello world") 三个双引号也可以注释多行 """ pri ...

  7. HTML的基本知识点

    <!DOCTYPE HTML> <html> <body> <video width="320" height="240&quo ...

  8. Eloquent Attach/Detach/Sync Fires Any Event

    eloquent-attach-detach-sync-fires-any-event I have a laravel project, and I need to make some calcul ...

  9. python Trie树和双数组TRIE树的实现. 拥有3个功能:插入,删除,给前缀智能找到所有能匹配的单词

    #coding=utf- #字典嵌套牛逼,别人写的,这样每一层非常多的东西,搜索就快了,树高26.所以整体搜索一个不关多大的单词表 #还是O(). ''' Python 字典 setdefault() ...

  10. mysql Error_code: 1593

    mysql slave_IO_running no  查看data目录下的.err文件提示:mysql  Error_code: 1593 解决方法: 1.检查 data目录下auto.cnf 文件中 ...