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 ...
随机推荐
- ROS节点理解--5
理解 ROS节点(原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/) Description: 本教程主要介绍 ROS 图(graph)概念 并讨 ...
- Ubuntu 建立桌面快捷方式
非常多时候,在Ubuntu下软件能够通过命令安装.可是有时候比較方便的方法就是下载一个压缩包,解压就能够使用了. 可是每次使用都要先跳到解压文件夹.这就比較麻烦.所以能够通过创建桌面快捷方式来解决问题 ...
- SVN-001
1.cd到指定目录: 2.执行:svn cleanup:
- python文件和目录操作方法大全(含实例)【python】
转自:http://www.jb51.net/article/48001.htm 一.python中对文件.文件夹操作时经常用到的os模块和shutil模块常用方法. 1.得到当前工作目录,即当前Py ...
- Python cookielib 模块
什么是 cookie : 指某些网站为了辨别用户身份,进行 session 跟踪而储存在用户本地终端上的数据,通常以 txt 文件形式存储.比如你登录了淘宝,浏览器就会保存 cookie 信息,这样我 ...
- poj_2406 kmp
题目大意 给出一个字符串S,S可能是多个较短的字符串的重复连接.比如"ababab"为字符串"ab"重复三次的结果:"abc" 为" ...
- poj_2182 线段树/树状数组
题目大意 n个数排成一排(不知道大小,只是占了一个位置),从a[1]到a[n]进行遍历,对于每个a[i],给出从a[1]到a[i-1]中小于a[i]数的个数.要求出 a[1]到a[n]中这n个数的相对 ...
- devstack screen 详解
n my previous blog i discussed how to install devstack based openstack. Now if I need to restart ind ...
- jQuery的无new构建
正常面向对象的写法: var cJquery = function(){ //构造函数体 }; cJquery.prototype = { name : function(alert("ch ...
- chorme快捷键
Chrome窗口和标签页快捷键:Ctrl+N 打开新窗口 Ctrl+T 打开新标签页 Ctrl+Shift+N 在隐身模式下打开新窗口 Ctrl+O,然后选择文件 在谷歌浏览器中打开计算机上的文件 按 ...