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 10​4​​. 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[模拟][螺旋矩阵][难]的更多相关文章

  1. [Leetcode] spiral matrix ii 螺旋矩阵

    Given an integer n, generate a square matrix filled with elements from 1 to n 2 in spiral order. For ...

  2. 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 ...

  3. [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 ...

  4. [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 ...

  5. [LeetCode] Spiral Matrix II 螺旋矩阵之二

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  6. PAT 1105 Spiral Matrix

    This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasi ...

  7. 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 ...

  8. Leetcode59. Spiral Matrix II螺旋矩阵2

    给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, ...

  9. leetcode-Spiral Matrix II 螺旋矩阵2之python大法好,四行就搞定,你敢信?

    Spiral Matrix II 螺旋矩阵 Given an integer n, generate a square matrix filled with elements from 1 to n2 ...

随机推荐

  1. VS 最近打开清理bat

    VS2008RecentCleaner.bat @echo off @REG Delete HKCU\Software\Microsoft\VisualStudio\9.0\FileMRUList / ...

  2. mysql的引擎myisam和innodb的区别

    1. MYISAM和INNODB的不同?答:主要有以下几点区别:   a)构造上的区别     MyISAM在磁盘上存储成三个文件,其中.frm文件存储表定义:.MYD (MYData)为数据文件:. ...

  3. web.xml 中的listener、filter、servlet 加载顺序及其【配置详解】

    在项目中总会遇到一些关于加载的优先级问题,近期也同样遇到过类似的,所以自己查找资料总结了下,下面有些是转载其他人的,毕竟人家写的不错,自己也就不重复造轮子了,只是略加点了自己的修饰. 首先可以肯定的是 ...

  4. LeetCode——Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary t ...

  5. linux中的目录和文件的统计

    ls 目录 | wc -l find  ./   -type   d   |   wc   -l      //查找目录个数 find   ./   -type   f   |   wc   -l   ...

  6. Python cookielib 模块

    什么是 cookie : 指某些网站为了辨别用户身份,进行 session 跟踪而储存在用户本地终端上的数据,通常以 txt 文件形式存储.比如你登录了淘宝,浏览器就会保存 cookie 信息,这样我 ...

  7. linux系统UDP的socket通信编程

    发送方: /* * File: main.c * Author: tianshuai * * Created on 2011年11月29日, 下午10:34 * * 主要实现:发送20个文本消息,然后 ...

  8. thinkphp5 邮件发送(qq邮箱为例)

    一.首先需要你开启smtp 服务,登录qq邮箱,进入设置 -> 账户 注意: 开启后会生成授权码,一定要记下,两个都记下,登录邮件客户端需要.这里配置邮件发送也需要这个授权码 二. 下载phpm ...

  9. vux组件绑定事件

    我一开始是这样绑定事件的,但是没有效果: <box gap="15px 45px"> <x-button plain type="primary&quo ...

  10. iOS - 开发中调试小技巧

    对于软件开发而言,调试是必须学会的技能,重要性不言而喻.对于调试的技能,基本上是可以迁移的,也就是说你以前在其他平台上掌握的很多调试技巧,很多也是可以用在iOS开发中.不同语言.不同IDE.不同平台的 ...