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. Spring Boot学习笔记2——基本使用之最佳实践[z]

    前言 在上一篇文章Spring Boot 学习笔记1——初体验之3分钟启动你的Web应用已经对Spring Boot的基本体系与基本使用进行了学习,本文主要目的是更加进一步的来说明对于Spring B ...

  2. input 原生上传文件(type = file)

    1.表单上传文件的步骤: - 1)设置enctype 默认为:enctype="application/x-www-form-urlencoded"(一般不设置) 若要表单中有需要 ...

  3. jquery Jquery 遍历 获取设置 效果

    speed: slow fast 毫秒 隐藏 显示 $(selector).hide(speed,callback) 隐藏. $(selector).show(speed,callback) 显示 $ ...

  4. 9.11 h5日记

      9.11   超链接标签<a></a>十分特殊改a标签内容的字体颜色,必须是直接给a 设置,给它的父级标签设置是不可行的.   PS:什么是属性继承,即父级标签设置的样式后 ...

  5. 分析入口文件main.php

    在分析之前,需要了解php cli模式下的编程 1.了解getopt函数,php手册地址:http://php.net/manual/zh/function.getopt.php static pri ...

  6. django基础使用

    //创建应用 python3 manage.py startapp mysite //开启服务 python3 manage.py runserver 127.0.0.1:8080 //创建数据库命令 ...

  7. uuid唯一吗

    是唯一的.我在几台硬件完全相同(同一批购买的).软件也完全相同(用同一个GHOST系统安装)的电脑上试过:  不同的电脑上,wmic csproduct get uuid 获取的UUID码是不同的.另 ...

  8. Spring MVC 注解类型

    Spring 2.5 引入了注解 基于注解的控制器的优势 1. 一个控制器类可以处理多个动作,而一个实现了 Controller 接口的控制器只能处理一个动作 2. 基于注解的控制器的请求映射不需要存 ...

  9. Maven构建JavaWeb

    查看java和mvn版本 java -version mvn -v D:\software\yiibai\spring-1.4.3.RELEASE>java -versionjava versi ...

  10. JS原型与原型链

    1.原型(公用属性) 首先,我们来讲讲浏览器的初始状态,就是在无代码的情况下,浏览器所分配到的内存的使用情况,首先浏览器会创建一个全局对象global,而在这个全局对象global内含有许多的全局函数 ...