PAT (Basic Level) Practise (中文)1050. 螺旋矩阵(25)
来源:互联网 发布:网络营销优化 编辑:程序博客网 时间:2024/06/06 09:20
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个空格分隔,行末不得有多余空格。
输入样例:1237 76 20 98 76 42 53 95 60 81 58 93输出样例:
98 95 9342 37 8153 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;}
阅读全文
0 0
- PAT (Basic Level) Practise (中文)1050. 螺旋矩阵(25)
- PAT (Basic Level) Practise (中文) 1050. 螺旋矩阵(25)
- PAT (Basic Level) Practise (中文)1050. 螺旋矩阵(25)
- PAT BASIC LEVEL 1050. 螺旋矩阵(25)
- PAT (Basic Level) Practise (中文)
- PAT (Basic Level) Practise (中文)
- PAT (Basic Level) Practise (中文)--1001
- PAT (Basic Level) Practise (中文)
- PAT (Basic Level) Practise (中文)1001
- PAT (Basic Level) Practise (中文)1002
- PAT (Basic Level) Practise (中文) 1007
- PAT (Basic Level) Practise (中文)1015. 德才论 (25)
- PAT (Basic Level) Practise (中文)1020. 月饼 (25)
- PAT (Basic Level) Practise (中文)1030. 完美数列(25)
- PAT (Basic Level) Practise (中文)1020. 月饼 (25)
- PAT (Basic Level) Practise (中文)1020. 月饼 (25)
- PAT (Basic Level) Practise (中文)1030. 完美数列(25)
- PAT (Basic Level) Practise (中文)1045快速排序(25)
- Spark性能优化指南——基础篇
- windows10系统下安装keras框架以theano为后端并配置gpu加速
- 如何创建一个插件
- vim插件1--taglist
- 自定义View系列之ViewPager实现广告条效果
- PAT (Basic Level) Practise (中文)1050. 螺旋矩阵(25)
- ARP与RARP
- Highways UVA
- backpropagation
- tcp、ip协议概述
- Thinkphp5 微信公众号token验证不成功的原因
- spring+springMVC+hibernate事务配置
- 严蔚敏版数据结构学习笔记(1):线性表的顺序表示和实现
- HDU