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. Extjs如何添加多个Vtype

    http://devjs.eu/en/multiple-validation-multi-vtype-for-textfield-in-ext-js-4/

  2. Linux 任务管理 && 常用指令

    A.linux死机 转自:https://www.deleak.com/blog/2010/10/20/sysrq/ linux死机了怎么办? 曾经啊,对着键盘上 Print Screen/SysRq ...

  3. PAT 1061 判断题(15)(代码)

    1061 判断题(15 分) 判断题的评判很简单,本题就要求你写个简单的程序帮助老师判题并统计学生们判断题的得分. 输入格式: 输入在第一行给出两个不超过 100 的正整数 N 和 M,分别是学生人数 ...

  4. POJ3621或洛谷2868 [USACO07DEC]观光奶牛Sightseeing Cows

    一道\(0/1\)分数规划+负环 POJ原题链接 洛谷原题链接 显然是\(0/1\)分数规划问题. 二分答案,设二分值为\(mid\). 然后对二分进行判断,我们建立新图,没有点权,设当前有向边为\( ...

  5. gcc产生类型转换告警

    问题背景: 看 https://www.cnblogs.com/sinaxyz/p/4525208.html 这个篇blog时候,发现在应用层代码中,函数 int open_netlink() 中,有 ...

  6. Python之路番外(第二篇):PYTHON基本数据类型和小知识点

    一.基础小知识点 1.如果一行代码过长,可以用续行符 \换行书写 例子 if (signal == "red") and \ (car == "moving") ...

  7. sqli-labs:5-6,盲注

    思考1:当# --+都被过滤时,只能考虑闭合处理 思考2:union联合注入时必须先判断字段长度 eg. id=1' order by 3 and '1'='1 sqli5: 首先判断出对id经过了' ...

  8. xcode资源管理

    1. 在根目录放图片. UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ok.pn ...

  9. javascript对象bind()方法兼容处理

    bind() 函数在 ECMA-262 第五版才被加入:它可能无法在所有浏览器上运行.你可以部份地在脚本开头加入以下代码,就能使它运作,让不支持的浏览器也能使用 bind() 功能 if (!Func ...

  10. MMS从Contacts中添加收件人显示email账号

    android系统默认代码,MMS中可以添加email地址作为收件人,但是从Contacts中选择收件人时却不显示email. 解决思路:为了降低修改量,在原来只搜索phoneNum的基础上,再做一次 ...