A1105. Spiral Matrix
This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasing order. A spiral matrixis filled in from the first element at the upper-left corner, then move in a clockwise spiral. The matrix has m rows and ncolumns, 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
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
int G[][];
int N, num[], index = ;
bool cmp(int a, int b){
return a > b;
}
int main(){
scanf("%d", &N);
for(int i = ; i < N; i++)
scanf("%d", &num[i]);
int sqr = sqrt(N * 1.0);
int m, n;
for(n = sqr; N % n != ; n--);
m = N / n;
sort(num, num + N, cmp);
int k = , j = , times = ;
while(index < N){
if(N - index == ){
G[k][j] = num[index];
break;
}
while(j < n - - times && index < N){
G[k][j++] = num[index++];
}
while(k < m - - times && index < N){
G[k++][j] = num[index++];
}
while(j > times && index < N){
G[k][j--] = num[index++];
}
while(k > times && index < N){
G[k--][j] = num[index++];
}
k++;
j++;
times++;
}
for(int i = ; i < m; i++){
for(int j = ; j < n; j++){
if(j == n - )
printf("%d", G[i][j]);
else
printf("%d ", G[i][j]);
}
printf("\n");
}
cin >> N;
return ;
}
总结:
1、采用旋转填充二维数组的方式的时候要注意,当遇到图形中心是一个时会出现死循环,需要特殊处理一下。如图:
2、测试数据:测试只有1个元素、只有1列元素、边长为奇数的正方形、边长偶数的正方形。
3、超时有可能是因为出现了死循环,而不一定是复杂度不对。
4、方法,设顶点为(x,y),边长为m、n。填充一圈后变为顶点(x+1, y+1),边长变为m - 2, n - 2。
A1105. Spiral Matrix的更多相关文章
- 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 ...
- PAT_A1105#Spiral Matrix
Source: PAT A1105 Spiral Matrix (25 分) Description: This time your job is to fill a sequence of N po ...
- [LeetCode] Spiral Matrix II 螺旋矩阵之二
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- [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
54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...
- 【leetcode】Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- 【leetcode】Spiral Matrix II (middle)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- 59. Spiral Matrix && Spiral Matrix II
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- LeetCode:Spiral Matrix I II
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
随机推荐
- linux安装php7.2.7
1.下载php 官网下载:#wget http://cn2.php.net/get/php-7.2.7.tar.gz/from/a/mirror.(ps:应该是这么下载的,但是我下载的都是一个mirr ...
- Yii2控制台程序最佳实践
模板工程标准的控制台程序要素: (1)完整明确文字提示用户(并且使用红,绿,黄三种颜色标识提示文字:红色为错误相关,绿色为成功相关,黄色为进行中提示) (2)告知用户运行进度(完成任务的一部分即显示进 ...
- JAVA不可变类(immutable)机制与String的不可变性--非常好.
JAVA不可变类(immutable)机制与String的不可变性 https://www.cnblogs.com/jaylon/p/5721571.html
- LOADING Redis is loading the dataset in memory Redis javaAPI实例
今天在实现Redis客户端API操作Jedis的八种调用方式详解中,遇到了LOADING Redis is loading the dataset in memory错误,经过多番查找资料,找到了解决 ...
- onbeforeunload事件两种写法及效果
在符合W3C标准的浏览器里,可以使用addEventListener方法来添加事件. 当不需要为一个事件添加多个处理函数的时候,可以简单的使用onXXX=function(){}的方式来添加事件处理函 ...
- model,map,MapAndVivew用于页面跳转时候使用的即跳转后才添加属性 这样再回调中无法使用 因为回调的前提是页面不调转;解决的方法是用responsewrite(普通的字符响应)
model,map,MapAndVivew用于页面跳转时候使用的即跳转后才添加属性 这样再回调中无法使用 因为回调的前提是页面不调转:解决的方法是用responsewrite
- C 语言----- 指针
指针是一个值为内存地址的变量, 指针的核心是它是一个变量, 只不过它是用来存放内存地址的, 所以在了解指针之前,先说一下什么是变量.变量就是在内存中开辟的一个空间.如int year, 就是在内存中开 ...
- U盘快速启动热键
各个品牌电脑U盘快速启动热键如下:
- 浅析Android Dialog中setContentView()方法
2017-05-15 概述 Dialog在Android中是一个很优秀的工具.在使用Dialog时,我们一般都会自定义要显示的内容布局.Dialog自带了三个方法来支持自定义内容布局. public ...
- #189 stat(动态规划)
容易想到固定第一个排列为1~n,算出答案后乘上n!即可,但这样就离正解走远了. 考虑排列dp的一般套路,将数从大到小加入排列,这样每个位置第一次填数时(不管是第一个还是第二个排列)其贡献就确定了. 显 ...