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 ...
随机推荐
- <ReversingEngineering>关于windows32位系统下的dll注入技术经验汇
上个学期把自己闷在图书馆一直在看关于逆向工程技术方面的书,从入门到初级,现在也敢说自己一条腿已经迈进了这片知识的大门里,因为该博客刚开通先将一些经验记录下来,也是留给自己一方面做个参照. <逆向 ...
- 吴恩达机器学习笔记(八) —— 降维与主成分分析法(PCA)
主要内容: 一.降维与PCA 二.PCA算法过程 三.PCA之恢复 四.如何选取维数K 五.PCA的作用与适用场合 一.降维与PCA 1.所谓降维,就是将数据由原来的n个特征(feature)缩减为k ...
- JAVA- 清除数组重复元素
清除数组重复元素并打印新数组. import java.util.*; public class Repeat { public static void main(String[] args) { / ...
- 织梦CMS博客风格模板
织梦CMS博客风格模板,织梦CMS,博客模板,CMS模板.程序模板. 模板地址:http://www.huiyi8.com/sc/7248.html
- 从CWnd::GetSafeHwnd实现得到的知识
在看MFC源码的过程中,有个地方一直不解,看如下代码 BOOL CFrameWnd::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWO ...
- BZOJ3674:可持久化并查集加强版
浅谈主席树:https://www.cnblogs.com/AKMer/p/9956734.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem.p ...
- Oracle分组后取某列最大值的行数据
select * from ( select last_comment, row_number() over(partition by employeeid,roadline,stationname ...
- Ajax学习(1)
Web 1.0 它指的就是具有完全不同的请求和响应模型的传统 Web.比如,到 hdu.edu.cn 网站上点击一个按钮.就会对服务器发送一个请求,然后响应再返回到浏览器.该请求不仅仅是新内容和项目列 ...
- 4.xpath注入详解
0x01 简介 XPath注入攻击是指利用XPath 解析器的松散输入和容错特性,能够在 URL.表单或其它信息上附带恶意的XPath 查询代码,以获得权限信息的访问权并更改这些信息.XPath注入发 ...
- ACM-ICPC2018焦作网络赛 Mathematical Curse(dp)
Mathematical Curse 22.25% 1000ms 65536K A prince of the Science Continent was imprisoned in a cast ...