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. 在程序中使用命令行的方式来调用py文件

    做这个主要是程序可以做到直接调用一个脚本,而不是从脚本中把类或者函数import出来这样调用,比如我们写的python命令行文件,让java来调用,让c++来调用,都是可以的.这样不需要整个语言都用p ...

  2. 第五章 面向方面编程___AOP入门

    上一篇讲了 AOP 和 OOP 的区别,这一次我们开始入门 AOP .实现面向方面编程的技术,主要分为两大类: 一是 采用动态代理技术,利用截取消息的方式,对该消息进行装饰,以取代原有对象行为的执行: ...

  3. ASP.Net MVC开发基础学习笔记(7):数据查询页面

     前言 前面铺垫了那么多,今天我们就用MVC5 + EF6 + Bootstrap3来正式创建一个基本查询页面. 为什么从查询页面開始?嘿嘿.由于小弟的.Net生涯就是从查询页面開始的,记得正式工 ...

  4. ASP代码审计学习笔记 -3.上传漏洞

    1.ASP上传过程抓包分析: POST /4.asp HTTP/1.1 Host: 192.168.1.102 User-Agent: Mozilla/5.0 (Windows NT 10.0; WO ...

  5. 【渗透测试学习平台】 web for pentester -5.代码执行

    Example 1 http://192.168.106.154/codeexec/example1.php?name=".system('uname -a');// Example 2 h ...

  6. Swift-属性、方法、下标

    存储属性和计算属性 类.结构和枚举都能够定义存储属性和计算属性.其中存储属性就是常见的形式,又分为变量属性和常量属性,如: struct Point { var x = 0.0, y = 0.0 } ...

  7. web基础---->java邮件的发送

    这里记录一下关于java邮件发送代码的编写.你在我身边也好,在天边也罢,想到世界的角落有一个你,觉得整个世界也变得温柔安定了. java邮件的发送 一.直接贴出代码,如下: package com.c ...

  8. nohop以及后台运行的相关集合

    本文参考:https://blog.csdn.net/u011095110/article/details/78666833 1. 后台运行一个命令: & tar -czvf /mnt/aa. ...

  9. 【巷子】---vue路由懒加载---【vue】

    一.懒加载 也叫延迟加载或者按需加载,即在需要的时候进行加载,   二.为什么要使用懒加载 像vue这种单页面应用,如果没有应用懒加载,运用webpack打包后的文件将会异常的大,造成进入首页时,需要 ...

  10. (转)大数据量下的SQL Server数据库优化

     在SQL Server中,默认MDF文件初始大小为5MB,自增为1MB,不限增长,LDF初始为1MB,增长为10%,限制文件增长到一定的数目:一般设计中,使用SQL自带的设计即可,但是大型数据库设计 ...