PAT甲级——1105 Spiral Matrix (螺旋矩阵)
此文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90484058
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 (螺旋矩阵)的更多相关文章
- PAT 甲级 1105 Spiral Matrix
https://pintia.cn/problem-sets/994805342720868352/problems/994805363117768704 This time your job is ...
- Leetcode 54:Spiral Matrix 螺旋矩阵
54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of t ...
- [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(螺旋矩阵) 解题思路和方法
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- 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 ...
- [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 ...
- 第29题:LeetCode54:Spiral Matrix螺旋矩阵
给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ...
- 【LeetCode】Spiral Matrix(螺旋矩阵)
这是LeetCode里的第54道题. 题目要求: 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ...
- Leetcode54. Spiral Matrix螺旋矩阵
给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ...
随机推荐
- 版本名称SNAPSHOT、alpha、beta、release、GA含义
Alpha:是内部测试版,一般不向外部发布,会有很多Bug.一般只有测试人员使用.Beta:也是测试版,这个阶段的版本会一直加入新的功能.在Alpha版之后推出.RC:(Release Candida ...
- 创建Django博客的数据库模型
声明:此Django分类下的教程是追梦人物所有,地址http://www.jianshu.com/u/f0c09f959299,本人写在此只是为了巩固复习使用 blog最主要的功能就是展示我们写的文章 ...
- 分享知识-快乐自己:Hibernate 中的 HQL 语句的实际应用
概要: Hibernate 支持三种查询方式: HQL查询.Criteria查询及原声 SQL (Native SQL)查询. HQL(Hibernate Query Language,Hiberna ...
- Spring MVC文件上传下载工具类
import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import ...
- storm相关技术
There are two kinds of nodes on a Storm cluster: the master node and the worker nodes. 有两种节点,主节点和wor ...
- css 3d box 实现的一些注意事项
Test1.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- 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 ...
- 1068 Bash游戏 V3
1068 Bash游戏 V3 题目来源: Ural 1180 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 关注 有一堆石子共有N个.A B两个人轮流拿 ...
- 京东SDK模板卡盘效果实现代码
最近在做京东模板,因为是最新平台,好多功能都需要摸索,俺技术一般,摸索出一个简易的卡盘功能 ——————使用的是分类推荐模块哦! 本着共享的精神,俺将代码放到这儿了,各人请自便.(代码还不够完善, ...
- BZOJ3648:寝室管理
浅谈树分治:https://www.cnblogs.com/AKMer/p/10014803.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem. ...