此文同步发布在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:

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

题目大意:将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、若数据放完了则退出循环。

 #include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
vector <int> v;
int N,
index = ,
ans[][];
int getNum(int N);
bool cmp(int a, int b);
void leftToRight(int &left, int &right, int &top, int &bottom);
void topToBottom(int &left, int &right, int &top, int &bottom);
void rightToLeft(int &left, int &right, int &top, int &bottom);
void bottomToTop(int &left, int &right, int &top, int &bottom);
int main()
{
int m, n;
scanf("%d", &N);
v.resize(N);
for(int i = ; i < N; i++)
scanf("%d", &v[i]);
sort(v.begin(), v.end(), cmp);
n = getNum(N);
m = N / n;
int left = ,
right = n-,
bottom = m-,
top = ;
while(index < N){
leftToRight(left, right, top, bottom);
topToBottom(left, right, top, bottom);
rightToLeft(left, right, top, bottom);
bottomToTop(left, right, top, bottom);
}
for(int i = ; i < m; i++){
for(int j = ; j < n; j++){
printf("%d", ans[i][j]);
if(j < n-)
printf(" ");
}
printf("\n");
}
return ;
} void bottomToTop(int &left, int &right, int &top, int &bottom){
if(top > bottom || index >= N)
return;
for(int i = bottom; i>= top; i--){
ans[i][left] = v[index];
index++;
}
left++;
} void rightToLeft(int &left, int &right, int &top, int &bottom){
if(left > right || index >= N)
return;
for(int i = right; i >= left; i--){
ans[bottom][i] = v[index];
index++;
}
bottom--;
} void topToBottom(int &left, int &right, int &top, int &bottom){
if(top > bottom || index >= N)
return;
for(int i = top; i <= bottom; i++){
ans[i][right] = v[index];
index++;
}
right--;
} void leftToRight(int &left, int &right, int &top, int &bottom){
if(left > right || index >= N)
return;
for(int i = left; i <= right; i++){
ans[top][i] = v[index];
index++;
}
top++;
} int getNum(int N){
int n = sqrt(N);
while(n > ){
if(N % n == )
break;
n--;
}
return n;
}
bool cmp(int a, int b){
return a > b;
}

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. BZOJ 1231 [Usaco2008 Nov]mixup2 混乱的奶牛:状压dp + 滚动数组

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1231 题意: 给你n个数字s[i],问你有多少个排列,使得任意相邻两数字之差的绝对值大于m ...

  2. html5--1.19 通用属性

    html5--1.19 通用属性 学习要点: 1.通用属性的概念及几个常用的通用属性2.对属性值的若干点补充 通用属性 通用属性(全局属性)可以用于任何的HTML5元素:通用属性有十几种:这节课不会全 ...

  3. codeforces 659E E. New Reform(图论)

    题目链接: E. New Reform time limit per test 1 second memory limit per test 256 megabytes input standard ...

  4. Java se 知识图解

  5. SoundHound Inc. Programming Contest 2018

    A - F Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100100 points Problem Statement You are give ...

  6. jupyter-notebook重设项目工作路径

    一. . Anaconda Prompt 命令(方法没生效) 1 选择一个用于存放config文件的文件夹(先创建) 2 在cmd中进入该文件夹的路径 3在cmd中 输入​命令 jupyter not ...

  7. x264源代码分析-转

    相关说明: 1.     使用版本:  x264-cvs-2004-05-11 2.     这次的分析基本上已经将代码中最难理解的部分做了阐释,对代码的主线也做了剖析,如果这个主线理解了,就容易设置 ...

  8. ORA-00119: invalid specification for system parameter REMOTE_LISTENER

    环境说明:   RAC 启动数据库报 ORA-00119: invalid specification for system parameter REMOTE_LISTENER   . 检查 list ...

  9. 虚拟机VMware Workstation cannot connect to the virtual machine

    解决方法: 从提示消息我们可以看到,问题在于VMware授权服务没有开启,具体处理方法如下: "This PC(我的电脑)"---右键"manage(管理)"- ...

  10. 八 Vue学习 fetch请求

    1:import {login, getAdminInfo} from '@/api/getData'(从api/getData.js中import login函数.) 看一下如下的getData.j ...