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 ...
随机推荐
- oracle数据库触发器(trigger)用法总结
from:http://blog.csdn.net/zhanzhib/article/details/48729417 触发器的意思就是当我们对数据库对象(一般是表或视图)进行insert.updat ...
- help()
help() 用于查看函数或模块的帮助信息 In [1]: help(id) # 查看id()这个函数的帮助信息,注意不要写成help(id()) id(...) id(object) -> i ...
- Nutch URL过滤配置规则
nutch网上有不少有它的源码解析,但是采集这块还是不太让人容易理解.今天终于知道怎么,弄的.现在把crawl-urlfilter.txt文件贴出来,让大家一块交流,也给自己备忘录一个. # Lice ...
- 《C++ Primer Plus》15.4 RTTI 学习笔记
运行时类型识别RTTI(Runtime Type Identification) C++有三个支持RTTI的元素.* 如果可能的话,dynamic_cast运算符将使用一个指向基类的指针来生成一个指向 ...
- cocos2dx游戏--欢欢英雄传说--添加人物
接下来需要导入精灵帧资源,因为之前下载了TexturePacker,然后通过TexturePacker的"Publish sprite sheet"方法可以生成一个.pvr.ccz ...
- struts2基础---->自定义类型转换器
这一章,我们开始struts2中自定义类型转换器的学习. 自定义类型转换器
- tomcat源码---->request的请求参数分析
当contentType为application/json的时候,在servlet中通过request.getParameter得到的数据为空.今天我们就java的请求,分析一下request得到参数 ...
- C++虚继承的概念[转]
C++中虚拟继承的概念 为了解决从不同途径继承来的同名的数据成员在内存中有不同的拷贝造成数据不一致问题,将共同基类设置为虚基类.这时从不同的路径继承过来的同名数据成员在内存中就只有一个拷贝,同一个函数 ...
- activemq 实战 四 传输连接器-Transport connectors 4.2
In order to exchange messages, producers and consumers (clients) need to connect to the broker. This ...
- css3-巧用选择器 “:target”
今天(昨天)又发现一个知识盲区 css3的:target标签,之前学习的时候就是一眼扫过,说是认识了,但其实也就记了三分钟,合上书就全忘光了. 直到昨天,遇到一个有意思的题目,用css3新特性做一个类 ...