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 ...
随机推荐
- ACM学习历程—UESTC 1215 Secrete Master Plan(矩阵旋转)(2015CCPC A)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1215 题目大意就是问一个2*2的矩阵能否通过旋转得到另一个. 代码: #include <iostre ...
- 【LeetCode】020. Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- bzoj 3998 弦论 —— 后缀自动机
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3998 关于相同子串算一个还是算多个,其实就是看一种状态的 right 集合是否加上 Pare ...
- TextBlock截断字符显示为....
添加: TextTrimming="CharacterEllipsis" 到TextBlock中, 即可让TextBlock 支持截断字符显示为...
- POJ1088(记忆化搜索)
经典记忆化搜索题目.当 从每个点一次进行搜索时要采用 记忆化搜索 #include"cstdio" #include"algorithm" using name ...
- JVM插码之三:javaagent介绍及javassist介绍
本文介绍一下,当下比较基础但是使用场景却很多的一种技术,稍微偏底层点,就是字节码插庄技术了...,如果之前大家熟悉了asm,cglib以及javassit等技术,那么下面说的就很简单了...,因为下面 ...
- springMVC绑定json参数之二(2.2.1)
二.springmvc 接收不同格式的json字符串 2.扫盲完了继续测试springmvc 接收不同格式的json字符串 1).格式一:json简单数组对象 前台两种传递方式: 方式一(需要拼接js ...
- Redis IRedisTypedClient
IRedisTypedClient IRedisTypedClient类相当于IRedicClient的强类型版,其方法与属性大多数与IRedisClient类似. 它支持在Redis中使用Linq查 ...
- <正则吃饺子> :关于Java的native方法(转)
感谢作者的分享,原文地址:http://blog.csdn.net/wike163/article/details/6635321 一. 什么是Native Method 简单地讲,一个Nativ ...
- Windchill 查询功能
一.使用SearchCondition 查询语句中用容器中的containerReference.key.id名称来代替数据库中的字段idA3containerReference /** * ...