此文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90484058

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 n satisfy 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:

  1. 12
  2. 37 76 20 98 76 42 53 95 60 81 58 93

Sample Output:

  1. 98 95 93
  2. 42 37 81
  3. 53 20 76
  4. 58 60 76

题目大意:将N个数字降序顺时针螺旋放入一个矩阵,然后输出矩阵。矩阵的行列数分别为m、n,要求m*n=N 且 m≥n、m-n最小。

思路:n取N的平方根,若N能被n整除,则符合条件,若不能,则n--继续寻找。螺旋放入矩阵有四个边界:left、right、top、bottom;写四个函数在边界之内按照四个方向往矩阵放入数据,进入循环:1、从左往右,到达右边界 right 的时候,top++(上边界往下压一层);2、从上到下,到达底部,right++;3、从右往左,到达左边界,bottom++;4、从下往上,到达上边界,left++;5、若数据放完了则退出循环。

  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <cmath>
  5. using namespace std;
  6. vector <int> v;
  7. int N,
  8. index = ,
  9. ans[][];
  10. int getNum(int N);
  11. bool cmp(int a, int b);
  12. void leftToRight(int &left, int &right, int &top, int &bottom);
  13. void topToBottom(int &left, int &right, int &top, int &bottom);
  14. void rightToLeft(int &left, int &right, int &top, int &bottom);
  15. void bottomToTop(int &left, int &right, int &top, int &bottom);
  16. int main()
  17. {
  18. int m, n;
  19. scanf("%d", &N);
  20. v.resize(N);
  21. for(int i = ; i < N; i++)
  22. scanf("%d", &v[i]);
  23. sort(v.begin(), v.end(), cmp);
  24. n = getNum(N);
  25. m = N / n;
  26. int left = ,
  27. right = n-,
  28. bottom = m-,
  29. top = ;
  30. while(index < N){
  31. leftToRight(left, right, top, bottom);
  32. topToBottom(left, right, top, bottom);
  33. rightToLeft(left, right, top, bottom);
  34. bottomToTop(left, right, top, bottom);
  35. }
  36. for(int i = ; i < m; i++){
  37. for(int j = ; j < n; j++){
  38. printf("%d", ans[i][j]);
  39. if(j < n-)
  40. printf(" ");
  41. }
  42. printf("\n");
  43. }
  44. return ;
  45. }
  46.  
  47. void bottomToTop(int &left, int &right, int &top, int &bottom){
  48. if(top > bottom || index >= N)
  49. return;
  50. for(int i = bottom; i>= top; i--){
  51. ans[i][left] = v[index];
  52. index++;
  53. }
  54. left++;
  55. }
  56.  
  57. void rightToLeft(int &left, int &right, int &top, int &bottom){
  58. if(left > right || index >= N)
  59. return;
  60. for(int i = right; i >= left; i--){
  61. ans[bottom][i] = v[index];
  62. index++;
  63. }
  64. bottom--;
  65. }
  66.  
  67. void topToBottom(int &left, int &right, int &top, int &bottom){
  68. if(top > bottom || index >= N)
  69. return;
  70. for(int i = top; i <= bottom; i++){
  71. ans[i][right] = v[index];
  72. index++;
  73. }
  74. right--;
  75. }
  76.  
  77. void leftToRight(int &left, int &right, int &top, int &bottom){
  78. if(left > right || index >= N)
  79. return;
  80. for(int i = left; i <= right; i++){
  81. ans[top][i] = v[index];
  82. index++;
  83. }
  84. top++;
  85. }
  86.  
  87. int getNum(int N){
  88. int n = sqrt(N);
  89. while(n > ){
  90. if(N % n == )
  91. break;
  92. n--;
  93. }
  94. return n;
  95. }
  96. bool cmp(int a, int b){
  97. return a > b;
  98. }

PAT甲级——1105 Spiral Matrix (螺旋矩阵)的更多相关文章

  1. PAT 甲级 1105 Spiral Matrix

    https://pintia.cn/problem-sets/994805342720868352/problems/994805363117768704 This time your job is ...

  2. Leetcode 54:Spiral Matrix 螺旋矩阵

    54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of t ...

  3. [LeetCode] Spiral Matrix 螺旋矩阵

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

  4. leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法

    Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...

  5. PAT甲级——A1105 Spiral Matrix【25】

    This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasi ...

  6. [leetcode]54. Spiral Matrix螺旋矩阵

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

  7. 第29题:LeetCode54:Spiral Matrix螺旋矩阵

    给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ...

  8. 【LeetCode】Spiral Matrix(螺旋矩阵)

    这是LeetCode里的第54道题. 题目要求: 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ...

  9. Leetcode54. Spiral Matrix螺旋矩阵

    给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ...

随机推荐

  1. 版本名称SNAPSHOT、alpha、beta、release、GA含义

    Alpha:是内部测试版,一般不向外部发布,会有很多Bug.一般只有测试人员使用.Beta:也是测试版,这个阶段的版本会一直加入新的功能.在Alpha版之后推出.RC:(Release Candida ...

  2. 创建Django博客的数据库模型

    声明:此Django分类下的教程是追梦人物所有,地址http://www.jianshu.com/u/f0c09f959299,本人写在此只是为了巩固复习使用 blog最主要的功能就是展示我们写的文章 ...

  3. 分享知识-快乐自己:Hibernate 中的 HQL 语句的实际应用

    概要: Hibernate 支持三种查询方式: HQL查询.Criteria查询及原声 SQL (Native SQL)查询. HQL(Hibernate Query Language,Hiberna ...

  4. Spring MVC文件上传下载工具类

    import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import ...

  5. storm相关技术

    There are two kinds of nodes on a Storm cluster: the master node and the worker nodes. 有两种节点,主节点和wor ...

  6. css 3d box 实现的一些注意事项

    Test1.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  7. ACM学习历程——POJ3468 A Simple Problem with Integers(线段树)

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  8. 1068 Bash游戏 V3

    1068 Bash游戏 V3 题目来源: Ural 1180 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  关注 有一堆石子共有N个.A B两个人轮流拿 ...

  9. 京东SDK模板卡盘效果实现代码

    最近在做京东模板,因为是最新平台,好多功能都需要摸索,俺技术一般,摸索出一个简易的卡盘功能   ——————使用的是分类推荐模块哦! 本着共享的精神,俺将代码放到这儿了,各人请自便.(代码还不够完善, ...

  10. BZOJ3648:寝室管理

    浅谈树分治:https://www.cnblogs.com/AKMer/p/10014803.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem. ...