PAT 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 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:
12
37 76 20 98 76 42 53 95 60 81 58 93
Sample Output:
98 95 93
42 37 81
53 20 76
58 60 76
#include<iostream>
#include<vector>
#include<math.h>
#include<algorithm>
using namespace std;
bool cmp(const int& a, const int& b){
return a>b;
}
int main(){
int N, k;
cin>>N;
for(k=sqrt((double)N); k>=1; k--)
if(N%k==0)
break;
int row=N/k, col=k;
int n=row/2;
vector<int> vi(N,0);
for(int i=0; i<N; i++)
cin>>vi[i];
sort(vi.begin(), vi.end(), cmp);
vector<vector<int>> graph(row,vector<int>(col,0));
int cnt=0;
for(int i=0; i<=n; i++){
for(int j=i; j<col-i&&cnt<N; j++)
graph[i][j]=vi[cnt++];
for(int j=i+1; j<row-i&&cnt<N; j++)
graph[j][col-i-1]=vi[cnt++];
for(int j=col-i-2; j>=i&&cnt<N; j--)
graph[row-i-1][j]=vi[cnt++];
for(int j=row-2-i; j>=i+1&&cnt<N; j--)
graph[j][i]=vi[cnt++];
}
for(int i=0; i<row; i++){
for(int j=0; j<col; j++)
j==0?cout<<graph[i][j]:cout<<" "<<graph[i][j];
cout<<endl;
}
return 0;
}
PAT 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 分) ...
- 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 ...
- 1105 Spiral Matrix
This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasi ...
- 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 (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是素数时 ...
随机推荐
- 3301 Square words
3301 Square words 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description 定义s ...
- YTU 2769: 结构体--成绩统计
2769: 结构体--成绩统计 时间限制: 1 Sec 内存限制: 128 MB 提交: 1021 解决: 530 题目描述 建立一个简单的学生信息表,包括:姓名.性别.年龄及一门课程的成绩,统计 ...
- poj3737 UmBasketella 真正的三分
之前用二分写三分的板子...现在正式写一个三分,但是也不难,就是把区间分为三段就行了.求二次函数的峰值,每次取大的区间就行了. 题干: 最近几天,人们总是设计出多功能的新东西.例如,您不仅可以使用手机 ...
- 洛谷 p1625
高精度 我以为这题必有高论,怎么想也想不出来,没想到竟是如此粗鄙做法. 我们写一个高精度模拟一下,然后枚举约数看是否能约分,由于我不会高精度除法,就抄了一发 其实这种两项之比和项数有关的数列是不能推通 ...
- astgo常见问题(FAQ)知识库
Q:为什么我在astgo 的一些列表页面看不到右侧顶端的高级功能菜单?R:因为你没有先选择代理商,这些操作都是针对于某个代理商才可以操作的! Q:为什么我无法给astgo 的代理商充值?R:因为你登录 ...
- java线程系列---Runnable和Thread的区别 (转载)
转自:http://blog.csdn.net/wwww1988600/article/details/7309070 在java中可有两种方式实现多线程,一种是继承 Thread类,一种是实现Run ...
- PCB 录屏工具Screen2Exe GifCam ScreenToGif
我们完成的软件作品后,需要向客户或领导演示软件功能介绍,这里力推3款录屏工具 一.Screen2Exe工具,录制exe视频文件 下载地址 http://pcbren.cn/ShareFiles/Sc ...
- 织梦dedecms标签大全总结
织梦dedecms标签大全总结,同时还建议多参考dede默认模板,织梦默认模板上的标签还是很有参考价值的. dedecms系统参数全局标签,在后台系统设置里可以看到这个参数 网站名称:{dede:gl ...
- Behavior Designer扩展
BehaviorManager.instance.Tick(behaviorTree); 卸载update里u3d直接卡死 = = SharedVariable直接赋值会改变他的引用关系,必须用XXX ...
- MFC学习篇(二):error LNK2005 及其解决方法
环境:MFC条件下添加原有代码 >nafxcwd.lib(afxmem.obj) : error LNK2005: @YAPAXI@Z) already defined in LIBCMTD.l ...