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. JFinal上传文件时用getFile()方法报错

    原因是缺少cos.jar包,补上即可.

  2. netcore webapi 用户 'IIS APPPOOL\无托管代码' 登录失败

    配置在iis上,除了环境配置错误的原因还有一种可能是连接字符串的问题,iis要求使用sql server的sa或者其他登录用户. ps:连接字符串:  "Default": &qu ...

  3. svn-经常遇到问题解答办法积累(一)

    1.对于一个SVN使用新手,第一步,肯定是如何获取代码到本地指定的目录. 步骤: (1)新建一个存放svn中某一个代码库的目录,加入该目录命名为:Proj1SVN (2)右键鼠标,选择SVN Chec ...

  4. Hadoop集群配置(最全面总结 )(转)

    Hadoop集群配置(最全面总结) huangguisu 通常,集群里的一台机器被指定为 NameNode,另一台不同的机器被指定为JobTracker.这些机器是masters.余下的机器即作为Da ...

  5. iOS.Debug.Simulator

    1. iOS Simulator Tips & Tricks http://code.tutsplus.com/tutorials/ios-simulator-tips-tricks--mob ...

  6. LibreOJ 2003. 「SDOI2017」新生舞会 基础01分数规划 最大权匹配

    #2003. 「SDOI2017」新生舞会 内存限制:256 MiB时间限制:1500 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: 匿名 提交提交记录统计讨论测试数据   题目描述 ...

  7. 动态加载及Servlet容器加载

    动态加载 动态加载是 Servlet 3.0 中的新特性,它可以实现在不重启 Web 应用的情况下加载新的 Web 对象(Servlet.Filter.Listener). 为了实现动态加载的第一种方 ...

  8. pytho常用模块2——random

    random模块用来生成随机数,有以下几个常用方法: import random random.random() #产生随机数[0-1) random.randint(a,b) #产生随机整数[a,b ...

  9. c++11 多线程依次打印ABC

    并发 练习代码 #include <thread> #include <vector> #include <mutex> #include <iostream ...

  10. xcode资源管理

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