PAT 1105 Spiral Matrix[模拟][螺旋矩阵][难]
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 mrows 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 104. 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,并且有N个数,给出一个非递增的螺旋矩阵,m是行,n是列,m*n=N,要求m>=n,并且在所有可能的取值中,m-n的值最小。
//1.首先就需要对N进行因式分解,确定m和n。2.其次就放就可以了。
//N的范围没给。 不太明白螺旋矩阵的下标是什么规律。需要先学习一下螺旋矩阵的算法。
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
int func(int N) {
int i = sqrt((double)N);
while(i >= ) {
if(N % i == )//i是这样递减的,那么肯定能够保证是差值最小的了。
return i;
i--;
}
return ;
}
int cmp(int a, int b) {return a > b;}
int main() {
int N, m, n, t = ;
scanf("%d", &N);
n = func(N);//如果是12,那么n=3;
m = N / n;
vector<int> a(N);
for (int i = ; i < N; i++)
scanf("%d", &a[i]);
sort(a.begin(), a.end(), cmp);//从大到小排列。
vector<vector<int> > b(m, vector<int>(n));//m行,这么多列。
int level = m / + m % ;
for (int i = ; i < level; i++) {//i表示是第几圈。
for (int j = i; j <= n - - i && t <= N - ; j++)//t是控制原数组不越界。
b[i][j] = a[t++];//右
for (int j = i + ; j <= m - - i && t <= N - ; j++)
b[j][n - - i] = a[t++];//下
for (int j = n - i - ; j >= i && t <= N - ; j--)
b[m - - i][j] = a[t++];//左
for (int j = m - - i; j >= i + && t <= N - ; j--)
b[j][i] = a[t++];//上
}
for (int i = ; i < m; i++) {
for (int j = ; j < n; j++) {
printf("%d", b[i][j]);
if (j != n - ) printf(" ");
}
printf("\n");
}
return ;
}
//看了这个代码,还是稍微有点不太明白,需要复习,一下。
坐标的起始点和控制范围,
首先是向右的时候 :第几圈就是那个起始点,那么列的范围就是n-1-i;向下的时候,行的范围就是i+1开始,n-1-i列,这个需要多去实现一下。
PAT 1105 Spiral Matrix[模拟][螺旋矩阵][难]的更多相关文章
- [Leetcode] spiral matrix ii 螺旋矩阵
Given an integer n, generate a square matrix filled with elements from 1 to n 2 in spiral order. For ...
- 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 ...
- [LeetCode] 885. Spiral Matrix III 螺旋矩阵之三
On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east. Here, the north ...
- [LeetCode] 59. Spiral Matrix II 螺旋矩阵 II
Given an integer n, generate a square matrix filled with elements from 1 to n^2 in spiral order. For ...
- [LeetCode] Spiral Matrix II 螺旋矩阵之二
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- PAT 1105 Spiral Matrix
This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasi ...
- LeetCode OJ:Spiral Matrix(螺旋矩阵)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- Leetcode59. Spiral Matrix II螺旋矩阵2
给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, ...
- leetcode-Spiral Matrix II 螺旋矩阵2之python大法好,四行就搞定,你敢信?
Spiral Matrix II 螺旋矩阵 Given an integer n, generate a square matrix filled with elements from 1 to n2 ...
随机推荐
- 工作流JBPM_day01:2-HelloWorld
工作流JBPM_day01:2-HelloWorld 新建一个java工程Jbpm4.4 拷贝helloworld.jpdl.xml和helloworld.png文件出来压缩成helloworld.z ...
- 08python之列表的常用方法
列表list是python常用的数据类型,需要掌握以下常用方法: name_list = ['alex','tenglan','65brother'] 这个变量和之前的变量只存一个数字或字符串,这个列 ...
- 未配置jdk环境变量,cmd环境能运行java -version命令
我的情况是C:\Windows\System32路径下有java.exe.javaw.exe.javaws.exe三个文件,将三个文件删除后配置自己的jdk环境变量 可能原因参考帖子:https:// ...
- ZooKeeper(四)-- 第三方客户端 ZkClient的使用
前言 zkClient主要做了两件事情: 一件是在session loss和session expire时自动创建新的ZooKeeper实例进行重连. 另一件是将一次性watcher包装为持久watc ...
- Android中的动画,选择器,样式和主题的使用
一.动画: 1.动画的分类: 1).Tween动画:这种实现方式可以使视图组件移动.放大.缩小以及产生透明度的变化: 2).Frame动画:传统的动画方法,通过顺序的播放排列好的图片来实现,类似电影. ...
- C 环境设置(转自菜鸟教程)
C 环境设置 本地环境设置 如果您想要设置 C 语言环境,您需要确保电脑上有以下两款可用的软件,文本编辑器和 C 编译器. 文本编辑器 这将用于输入您的程序.文本编辑器包括 Windows Notep ...
- 怎样安装Linux操作系统
linux是免费的.其实这里的免费只是说linux的内核免费.在linux内核的基础上而产生了众多linux的版本.Linux的发行版说简单点就是将Linux内核与应用软件做一个打包.较知名的发行版有 ...
- C++ 在继承中使用virtual
使用virtual:如果方法是通过引用类型或指针而不是对象调用的,它将确定使用哪一种方法.如果没有使用关键字irtual,程序将根据引用类型或指针类型选择方法:如果使用了irtual,程序将根据引用或 ...
- 微信开放平台全网发布时,检测失败 —— C#
主要就是三个:返回API文本消息,返回普通文本消息,发送事件消息 --会出现失败的情况 (后续补充说明:出现检测出错,不一定是代码出现了问题,也有可能是1.微信方面检测时出现服务器请求失败,2.我 ...
- .Net内存溢出 System.OutOfMemoryException
内存溢出常见的情况和处理方式: http://outofmemory.cn/c/dotNet-outOfMemoryException MSDN中关于processModel的文档 https://m ...