PAT_A1105#Spiral Matrix
Source:
Description:
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×nmust 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
Keys:
- 简单模拟
Attention:
- 基本思路就是把序列按照从大到小的顺序,依次填入矩阵中;
- 直接开1e4的矩阵会超出内存,而且样例中存在m较大,n较小的情况,因此用一维数组代替二维数组
- 循环内的四个for循环需要判断pt<N
Code:
/*
Data: 2019-06-08 16:12:47
Problem: PAT_A1105#Spiral Matrix
AC: 01:05:29 题目大意:
序列从大到小,顺时针放入矩阵中
*/
#include<functional>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int M=1e4+;
int matrix[M], a[M]; int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif int N,n,m;
scanf("%d", &N);
for(int i=; i<N; i++)
scanf("%d", &a[i]);
for(int i=(int)sqrt((double)N); i>=; i--)
{
if(N%i == )
{
n = i;
m = N/i;
break;
}
}
sort(a,a+N,greater<int>());
fill(matrix,matrix+M,);
int pt=,c=,r=,R=m,C=n;
while(pt < N)
{
for(int i=c; i<=n && pt<N; i++)
matrix[C*r-+i-]=a[pt++];
r++;
for(int i=r; i<=m && pt<N; i++)
matrix[C*i-+n-]=a[pt++];
n--;
for(int i=n; i>=c && pt<N; i--)
matrix[C*m-+i-]=a[pt++];
m--;
for(int i=m; i>=r && pt<N; i--)
matrix[C*i-+c-]=a[pt++];
c++;
}
for(int i=; i<=R; i++)
for(int j=; j<=C; j++)
printf("%d%c", matrix[C*i-+j-], j==C?'\n':' '); return ;
}
PAT_A1105#Spiral Matrix的更多相关文章
- [LeetCode] Spiral Matrix II 螺旋矩阵之二
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- [LeetCode] Spiral Matrix 螺旋矩阵
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- LeetCode - 54. Spiral Matrix
54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...
- 【leetcode】Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- 【leetcode】Spiral Matrix II (middle)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- 59. Spiral Matrix && Spiral Matrix II
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- LeetCode:Spiral Matrix I II
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- Java for LeetCode 059 Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- Leetcode#59 Spiral Matrix II
原题地址 相比于Spiral Matrix(参见这篇文章)要简单一些,因为是方阵,所以代码简洁一些. 注意当n是奇数的时候,中心小块要单独赋值(代码21行) 代码: vector<vector& ...
随机推荐
- input的disabled和readonly区别
<input name=”country” id=”country” size=12 value=”disabled提交时得不到该值" disabled=”disabled” > ...
- EasyUI 在textbox里面输入数据敲回车后查询和普通在textbox输入数据敲回车的区别
EasyUI实现回车键触发事件 $('#id').textbox('textbox').keydown(function (e) { if (e.keyCode == 13) { alert('ent ...
- Android颜色透明度数值一览
100% — FF 95% — F2 90% — E6 85% — D9 80% — CC 75% — BF 70% — B3 65% — A6 60% — 99 55% — 8C 50% — 80 ...
- UVALive - 6910 (离线逆序并查集)
题意:给处编号从1~n这n个节点的父节点,得到含有若干棵树的森林:然后再给出k个操作,分两种'C x'是将节点x与其父节点所连接的支剪短:'Q a b'是询问a和b是否在同一棵树中. 题解:一开始拿到 ...
- Codeforces Round #388 (Div. 2) C. Voting
题意:有n个人,每个人要么是属于D派要么就是R派的.从编号1开始按顺序,每个人都有一次机会可以剔除其他任何一个人(被剔除的人就不在序列中也就失去了剔除其他人的机会了):当轮完一遍后就再次从头从仅存的人 ...
- luogu2467 [SDOI2010]地精部落
题目大意 求在$[1,n]$的排列中是波动序列的数量. 题解 性质 当我们对波动序列$a$进行以下操作时,得到的新序列仍然是个波动序列: 若$a_i = a_j+1且|j-i|>1$,将$a_i ...
- 将TensorFlow模型变为pb——官方本身提供API,直接调用即可
TensorFlow: How to freeze a model and serve it with a python API 参考:https://blog.metaflow.fr/tensorf ...
- WebSocket在Asp.Net中的例子
环境 以下代码环境要求:win8或win10, .net4.5+IIS8 部署到IIS8上面 转到 Windows程序和功能 -打开Windows功能里面 IIS选项启动4.5 和WebSocket支 ...
- B2241 打地鼠 暴力模拟
大水题!!!30分钟AC(算上思考时间),直接模拟就行,加一个判断约数的剪枝,再多加几个剪枝就可以过(数据巨水) 我也就会做暴力的题了. 题干: Description 打地鼠是这样的一个游戏:地面上 ...
- P4135 作诗——分块
题目:https://www.luogu.org/problemnew/show/P4135 分块大法: 块之间记录答案,每一块记录次数前缀和: 注意每次把桶中需要用到位置赋值就好了: 为什么加了特判 ...