Source:

PAT A1105 Spiral Matrix (25 分)

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的更多相关文章

  1. [LeetCode] Spiral Matrix II 螺旋矩阵之二

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  2. [LeetCode] Spiral Matrix 螺旋矩阵

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  3. LeetCode - 54. Spiral Matrix

    54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...

  4. 【leetcode】Spiral Matrix II

    Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...

  5. 【leetcode】Spiral Matrix II (middle)

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. Leetcode#59 Spiral Matrix II

    原题地址 相比于Spiral Matrix(参见这篇文章)要简单一些,因为是方阵,所以代码简洁一些. 注意当n是奇数的时候,中心小块要单独赋值(代码21行) 代码: vector<vector& ...

随机推荐

  1. JQuery获取select选中值和清除选中状态(转)

    1.获取值 var provinceSearch = $("#loc_province_search").find("option:selected").att ...

  2. 通过urllib2抓取网页内容(1)

    一.urllib2发送请求 import urllib2 url = 'http://www.baidu.com' req = urllib2.Request(url) response = urll ...

  3. vim快速操作

    简明 VIM 练级攻略 vim的学习曲线相当的大(参看各种文本编辑器的学习曲线),所以,如果你一开始看到的是一大堆VIM的命令分类,你一定会对这个编辑器失去兴趣的.下面的文章翻译自<Learn ...

  4. SSH整合开发时Scope为默认时现象与原理

    1.前提知识 1)scope默认值 进行SSH整合开发时,Struts2的action须要用spring容器进行管理,仅仅要涉及到类以bean的形式入到spring容器中.无论是xml配置还是使用注解 ...

  5. android之GMS认证

    来到了新的公司,才知道做手机是须要做GMS认证的.于是从一个从没有做过GMS认证的小白到一个月做了8个项目的GMS认证.最后.自己都是吐了.每天晚上都是一个人傻傻在加班.更是知道了高通的支持力度让人发 ...

  6. Advapi32.dll 函数接口说明

    Advapi32.dll 函数接口说明             函数原型                               说明    AbortSystemShutDown         ...

  7. android自定义dialog中点击listview的item事件关闭dialog

    import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; ...

  8. bzoj 1556 墓地秘密 —— 状压DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1556 预处理出两个障碍四个方向之间的距离(转弯次数),就可以状压DP了: 但预处理很麻烦.. ...

  9. bzoj2503 相框——思路

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2503 思路题: 首先,这种问题应该注意到答案只跟度数有关,跟其他什么连接方法之类的完全无关: ...

  10. java的random生成某个范围内的随机数

    import java.util.Random; /** * @author HP * @date 2019/4/16 */ public class randomTest { public stat ...