1105 Spiral Matrix
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 104. 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的更多相关文章
- PAT 1105 Spiral Matrix[模拟][螺旋矩阵][难]
1105 Spiral Matrix(25 分) This time your job is to fill a sequence of N positive integers into a spir ...
- PAT甲级——1105 Spiral Matrix (螺旋矩阵)
此文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90484058 1105 Spiral Matrix (25 分) ...
- 1105. Spiral Matrix (25)
This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasi ...
- PAT 甲级 1105 Spiral Matrix
https://pintia.cn/problem-sets/994805342720868352/problems/994805363117768704 This time your job is ...
- 1105 Spiral Matrix(25 分)
This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasi ...
- PAT 1105 Spiral Matrix
This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasi ...
- PAT甲题题解-1105. Spiral Matrix (25)-(模拟顺时针矩阵)
题意:给定N,以及N个数.找出满足m*n=N且m>=n且m-n最小的m.n值,建立大小为m*n矩阵,将N个数从大到下顺时针填入矩阵中. #include <iostream> #in ...
- PAT (Advanced Level) 1105. Spiral Matrix (25)
简单模拟. #include<cstdio> #include<cstring> #include<cmath> #include<map> #incl ...
- 【PAT甲级】1105 Spiral Matrix (25分)
题意:输入一个正整数N(实则<=1e5),接着输入一行N个正整数(<=1e4).降序输出螺旋矩阵. trick: 测试点1,3运行超时原因:直接用sqrt(N)来表示矩阵的宽会在N是素数时 ...
随机推荐
- WebAPI支持Session
1.在App_Start/WebApiConfig.cs中建立建立HttpControllerHandler和HttpControllerRouteHandler 并覆写它: public class ...
- vue2.0插件
1.better-scroll 参考网址:https://ustbhuangyi.github.io/better-scroll/doc/zh-hans/ better-scroll 是什么 firs ...
- 操作系统的发展史 day36
什么是操作系统 可能很多人都会说,我们平时装的windows7 windows10都是操作系统,没错,他们都是操作系统.还有没有其他的? 想想我们使用的手机,Google公司的Androi ...
- 1、javaweb学习之配置文件web.xml
今天这里主要讲述javaweb中的配置文件web.xml中的内容及其作用,都是基础部分,对于初学者需要好好掌握理解. 简单配置: <servlet> <servlet-name ...
- Linux CentOS 7 下 Apache Tomcat 7 安装与配置
前言 记录一下Linux CentOS 7安装Tomcat7的完整步骤. 下载 首先需要下载tomcat7的安装文件,地址如下: http://mirror.bit.edu.cn/apache/tom ...
- hdu 4004 (二分加贪心) 青蛙过河
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4004 题目意思是青蛙要过河,现在给你河的宽度,河中石头的个数(青蛙要从石头上跳过河,这些石头都是在垂 ...
- 三种简单排序算法(java实现)
一.冒泡排序 算法思想:遍历待排序的数组,每次遍历比较相邻的两个元素,如果他们的排列顺序错误就交换他们的位置,经过一趟排序后,最大的元素会浮置数组的末端.重复操 作 ...
- MySQL优化(一) 优化关键技术
MySql的优化是一个综合性的技术,主要包括有: (1)表的设计合理化(符合 3NF 三范式) (2)添加适当的索引(Index):索引分类:普通索引.主键索引.唯一索引.全文索引(文本).空间索引. ...
- 关于RNA-Seq数据去接头(Adapter)这事需要讲一讲
关于RNA-Seq数据去接头(Adapter)这事需要讲一讲 RNA-Seq adapter barcode cutadapt 首先来了解一下三个概念: 1.adapter是一段短的序列已知的核酸链, ...
- mysql 压缩方法
show global variables like 'innodb_file_format%';alter table t row_format=COMPRESSED;