PAT_A1105#Spiral Matrix
Source:
Description:
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×nmust 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
Keys:
- 简单模拟
Attention:
- 基本思路就是把序列按照从大到小的顺序,依次填入矩阵中;
- 直接开1e4的矩阵会超出内存,而且样例中存在m较大,n较小的情况,因此用一维数组代替二维数组
- 循环内的四个for循环需要判断pt<N
Code:
/*
Data: 2019-06-08 16:12:47
Problem: PAT_A1105#Spiral Matrix
AC: 01:05:29 题目大意:
序列从大到小,顺时针放入矩阵中
*/
#include<functional>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int M=1e4+;
int matrix[M], a[M]; int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif int N,n,m;
scanf("%d", &N);
for(int i=; i<N; i++)
scanf("%d", &a[i]);
for(int i=(int)sqrt((double)N); i>=; i--)
{
if(N%i == )
{
n = i;
m = N/i;
break;
}
}
sort(a,a+N,greater<int>());
fill(matrix,matrix+M,);
int pt=,c=,r=,R=m,C=n;
while(pt < N)
{
for(int i=c; i<=n && pt<N; i++)
matrix[C*r-+i-]=a[pt++];
r++;
for(int i=r; i<=m && pt<N; i++)
matrix[C*i-+n-]=a[pt++];
n--;
for(int i=n; i>=c && pt<N; i--)
matrix[C*m-+i-]=a[pt++];
m--;
for(int i=m; i>=r && pt<N; i--)
matrix[C*i-+c-]=a[pt++];
c++;
}
for(int i=; i<=R; i++)
for(int j=; j<=C; j++)
printf("%d%c", matrix[C*i-+j-], j==C?'\n':' '); return ;
}
PAT_A1105#Spiral Matrix的更多相关文章
- [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 ...
- Java for LeetCode 059 Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- Leetcode#59 Spiral Matrix II
原题地址 相比于Spiral Matrix(参见这篇文章)要简单一些,因为是方阵,所以代码简洁一些. 注意当n是奇数的时候,中心小块要单独赋值(代码21行) 代码: vector<vector& ...
随机推荐
- ASP.NET MVC 源码分析(一)
ASP.NET MVC 源码分析(一) 直接上图: 我们先来看Core的设计: 从项目结构来看,asp.net.mvc.core有以下目录: ActionConstraints:action限制相关 ...
- [ javascript ] getElementsByClassName与className和getAttribute!
对于javascript中的getElementsByClassName 在IE 6/7/8 不支持问题. 那么须要模拟出getElementsByClassName 须要採用className属性 ...
- PHP array_intersect()
定义和用法 array_intersect() 函数返回两个或多个数组的交集数组. 结果数组包含了所有在被比较数组中,也同时出现在所有其他参数数组中的值,键名保留不变. 注释:仅有值用于比较. 语法 ...
- PHP array_diff_ukey()
定义和用法 array_diff_ukey() 返回一个数组,该数组包括了所有出现在 array1 中但是未出现在任何其它参数数组中的键名的值.注意关联关系保留不变.与 array_diff() 不同 ...
- saprk里面的action - aggregate
上一篇讲到了spark里面的action函数: Action列表: reduce collect count first take takeSample takeOrdered saveAsTextF ...
- 3.0 - remote access 基础知识
RA概述: remote access: 广域网的远程连接,按L1分类: 1:通过电路交换网络实现的专线:(circuit switching) ~~~~~~~~~~~~~~~~~~~~~~~~~~~ ...
- Android使用GestureDetector实现手势滑动效果
直接看实例: package com.example.gesturedetector; import android.os.Bundle; import android.app.Activity; i ...
- Python爬虫抓取csdn博客
昨天晚上为了下载保存某位csdn大牛的所有博文,写了一个爬虫来自己主动抓取文章并保存到txt文本,当然也能够 保存到html网页中. 这样就能够不用Ctrl+C 和Ctrl+V了,很方便.抓取别的站点 ...
- hadoop常见操作命令
1.查看指定文件夹下内容 hadoop dfs –ls [文件文件夹] eg: hadoop dfs –ls /user/wangkai.pt 2.打开某个已存在文件 hadoop dfs –cat ...
- 创建虚拟机中的nova-scheduler 调度配置
一个Openstack 系统中通常包括多个计算节点 root@controller:/etc/nova# nova hypervisor-list +----+-------------------- ...