PAT (Basic Level) Practise (中文)1050. 螺旋矩阵(25)
2017-11-11 23:41
387 查看
1050. 螺旋矩阵(25)
时间限制150 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue
本题要求将给定的N个正整数按非递增的顺序,填入“螺旋矩阵”。所谓“螺旋矩阵”,是指从左上角第1个格子开始,按顺时针螺旋方向填充。要求矩阵的规模为m行n列,满足条件:m*n等于N;m>=n;且m-n取所有可能值中的最小值。
输入格式:
输入在第1行中给出一个正整数N,第2行给出N个待填充的正整数。所有数字不超过104,相邻数字以空格分隔。
输出格式:
输出螺旋矩阵。每行n个数字,共m行。相邻数字以1个空格分隔,行末不得有多余空格。
输入样例:
12 37 76 20 98 76 42 53 95 60 81 58 93
输出样例:
98 95 93 42 37 81 53 20 7658 60 76
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn=10000+10;
int a[maxn],mat[maxn][maxn];
bool cmp(int a,int b){
return a>b;
}
int main(){
int N;
scanf("%d",&N);
for(int i=0;i<N;i++) scanf("%d",&a[i]);
if(N==1){
printf("%d",a[0]);
return 0;
}
sort(a,a+N,cmp);
int m=(int)ceil(sqrt(1.0*N));
while(N%m!=0) m++;
int n=N/m,i=1,j=1,now=0;
int U=1,D=m,L=1,R=n;
while(now<N){
while(now<N&&j<R) mat[i][j++]=a[now++];
while(now<N&&i<D) mat[i++][j]=a[now++];
while(now<N&&j>L) mat[i][j--]=a[now++];
while(now<N&&i>U) mat[i--][j]=a[now++];
U++,D--,L++,R--;
i++,j++;
if(now==N-1) mat[i][j]=a[now++];
}
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++){
printf("%d",mat[i][j]);
if(j<n) printf(" ");
else printf("\n");
}
}
return 0;
}
相关文章推荐
- PAT (Basic Level) Practise (中文) 1050. 螺旋矩阵(25)
- PAT (Basic Level) Practise (中文)1050. 螺旋矩阵(25)
- PAT BASIC LEVEL 1050. 螺旋矩阵(25)
- PAT (Basic Level) Practise (中文)-1035. 插入与归并(25)
- PAT (Basic Level) Practise (中文)1030. 完美数列(25)
- PAT (Basic Level) Practise (中文)1020. 月饼 (25) C语言
- PAT Basic 1050. 螺旋矩阵(25)(C语言实现)
- PAT (Basic Level) Practise (中文)1030. 完美数列(25)
- PAT (Basic Level) Practise (中文)- 1010. 一元多项式求导 (25)
- PAT (Basic Level) Practise (中文)1005. 继续(3n+1)猜想 (25)
- PAT (Basic Level) Practise (中文)1025. 反转链表 (25)
- PAT (Basic Level) Practise (中文)1045. 快速排序(25)
- PAT (Basic Level) Practise (中文)1055. 集体照 (25)
- PAT(Basic Level) Practise (中文)1005. 继续(3n+1)猜想 (25)
- PAT basic-level 1050 螺旋矩阵 笔记
- PAT (Basic Level) Practise (中文)1015. 德才论 (25)
- PAT (Basic Level) Practise (中文)1060. 爱丁顿数(25)
- PAT (Basic Level) Practise (中文)1045. 快速排序(25) C语言
- PAT (Basic Level) Practise (中文) 1015. 德才论 (25)
- PAT (Basic Level) Practise (中文)1060. 爱丁顿数(25)