1105 Spiral Matrix(25 分)
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 nsatisfy 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 1. 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<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn = ; //数字不能太大
int matrix[maxn][maxn],A[maxn]; bool cmp(int a,int b){
return a > b;
} int main(){
int N;
scanf("%d",&N);
for(int i = ; i < N; i++){
scanf("%d",&A[i]);
}
if(N == ){
printf("%d",A[]);
return ;
}
sort(A,A+N,cmp);
int m = (int)ceil(sqrt(1.0*N));
while(N % m != ) m++; //除不整的时候m++
int n = N / m, i = , j = , now = ;
int U = , D = m, L = , R = n;
while(now < N){
while(now < N && j < R){
matrix[i][j] = A[now++];
j++;
}
while(now < N && i < D){
matrix[i][j] = A[now++];
i++;
}
while(now < N && j > L){
matrix[i][j] = A[now++];
j--;
}
while(now < N && i > U){
matrix[i][j] = A[now++];
i--;
}
U++,D--,L++,R--;
i++,j++;
if(now == N - ){
matrix[i][j] = A[now++];
}
}
for(int i = ; i <= m; i++){
for(int j = ; j <= n; j++){
printf("%d",matrix[i][j]);
if(j < n) printf(" "); //j < n,不是j < n - 1
else printf("\n");
}
}
return ;
}
1105 Spiral Matrix(25 分)的更多相关文章
- 【PAT甲级】1105 Spiral Matrix (25分)
题意:输入一个正整数N(实则<=1e5),接着输入一行N个正整数(<=1e4).降序输出螺旋矩阵. trick: 测试点1,3运行超时原因:直接用sqrt(N)来表示矩阵的宽会在N是素数时 ...
- 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 (螺旋矩阵)
此文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90484058 1105 Spiral Matrix (25 分) ...
- PAT 1105 Spiral Matrix[模拟][螺旋矩阵][难]
1105 Spiral Matrix(25 分) This time your job is to fill a sequence of N positive integers into a spir ...
- 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
https://pintia.cn/problem-sets/994805342720868352/problems/994805363117768704 This time your job is ...
- PAT 1105 Spiral Matrix
This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasi ...
随机推荐
- 自定义滚动条jQuery插件- Perfect Scrollbar
主要特性: 不需要修改任何的元素的css 滚动条不影响最初的页面布局设计 滚动条支持完整的自定义 滚动条的尺寸和位置会随着容器尺寸或者内容的变化而变化 依赖于jQuery和相关几个类库 不需要定义宽度 ...
- qtp重定义数组大小
a dim arr1() ) a dim arr() ReDim arr(a) arr arr ) arr For each i in arr print arr(i) Next
- k8s基础(3)etcd集群
下载安装 https://github.com/coreos/etcd/releases 在这网页,可以看到有多个版本共选择. 下载3.25 解压后, cd etcd-v3.2.5-linux-amd ...
- javascript之模拟块级作用域
在java.C++等语言中,变量i在会在for循环的语句块中定义,循环一旦结束,变量i就会被销毁.可是在javaScript中,从定义开始,就可以在函数内部随处访问.比如 function outpu ...
- 仿QQ底部切换(Fragment + Radio)
第一步: activity_main.xml 布局文件 <RelativeLayout xmlns:android="http://schemas.android.com/apk/ ...
- 关于android中两种service的编写简单总结
1.startservice (两种方法,继承service类或者继承intentservice 类) 继承service类,在onstartcommend重载方法中实现业务逻辑的处理,如果耗时过长最 ...
- 基本算法思想之递推算法思想(C++语言描述)
递推算法是非常常用的算法思想,在数学计算等场合有着广泛的应用.递推算法适合有明显公式规律的场合. 递推算法基本思想 递推算法是一种理性思维莫斯的代表,根据已有的数据和关系,逐步推到而得到结果.递推算法 ...
- win7中配置eclipse连接Ubuntu内的hadoop
Hadoop java 分布式 云计算 数据库 目录[-] 1.设置Ubuntu静态IP: 1)配置网卡参数到interfaces文件: 2)重启网卡: 2.配置Ubuntu的hosts: 3.配置V ...
- UCSC数据库数据调用cruzdb
https://github.com/Wy2160640/cruzdb UCSC基因组数据库是注释,调节和变异以及越来越多的分类群的各种数据的重要资源. 该库旨在简化数据的利用,以便我们可以进行复杂的 ...
- idea使用的知识
1. 如何设置,使IntelliJ IDEA智能提示忽略大小写. 很好用,在settings->Editor->General-->Code Completion里设置.2. ...