PAT (Basic Level) Practice (中文) 1050 螺旋矩阵 (25 分) 凌宸1642
PAT (Basic Level) Practice (中文) 1050 螺旋矩阵 (25 分)
题目描述
本题要求将给定的 N 个正整数按非递增的顺序,填入“螺旋矩阵”。所谓“螺旋矩阵”,是指从左上角第 1 个格子开始,按顺时针螺旋方向填充。
要求矩阵的规模为 m 行 n 列,满足条件:m×n 等于 N;m≥n;且 m−n 取所有可能值中的最小值。
输入格式:
输入在第 1 行中给出一个正整数 N,第 2 行给出 N 个待填充的正整数。所有数字不超过 10^4,相邻数字以空格分隔。
输出格式:
输出螺旋矩阵。每行 n 个数字,共 m 行。相邻数字以 1 个空格分隔,行末不得有多余空格。
输入样例:
12
37 76 20 98 76 42 53 95 60 81 58 93
输出样例:
98 95 93
42 37 81
53 20 76
58 60 76
题目要求:
作者 CHEN, Yue
单位 浙江大学
代码长度限制 16 KB
时间限制 400 ms
内存限制 64 MB
解题思路:
// 1、我们需要得到 螺旋矩阵 的行 row 与列 col 的值。
可以算出 n 的所有因子,然后找差值最小的两个因子。// 求出所有因子再计算差值有些复杂。
// 考虑到 相差最小的因子一定在 n 的平方根附近 若 n 是一个平方数,很明显最小差值的因子就是它的平方根
例如: n = 16 时,此时的 row = col = sqrt(n)
// 若 n 不是一个平方数,则从 n 的算术平方根 n0 开始往下枚举,枚举到 n 的一个因子的时候,即可得到符合题意的
// col 和 row 的值
例如: n = 10 时,sqrt(10) = 3 (因为col和row都是整型数据,自动取整) ,
但是 3 不是 10 的因子,所以往下枚举 2 , 2 是 10 的因子, 所以满足要求的 row = 5 ,col = 2 ( row >= col )
// 2、保存输入的 length 为 n 的数组,并从大到小排序
// 这里就是基础的 sort 函数运用了 ,存好数组,要求从大到小排序;手写一个与 sort 函数配合的 cmp 即可。
bool cmp(int a , int b){
return a > b ;
}
sort(array,array + array.length() , cmp) ;
// 我就是不想写 cmp 函数怎么办呢? 简单!
// 从大到小排序,我们知道,sort 函数是默认从小到大排序的,我把从小到大的数组,“倒”过来,不就是从大到小啦
sort(array,array + array.length()) ;
reverse(array,array + array.length()) ;
// 3、准备工作做好了,剩下就是怎么将数组里的数,按照螺旋矩阵的要求,给我螺旋进一个二维数组了。
// 数组中有 n 个数要填入螺旋矩阵,所以结束的标志很容易想到是,记录填入了 x 个数,当 x < n 的时候,说明还要填。
int ans[10010][110] = { 0 } , array[10010] ;
int i = 0 , j = 0 , x = 0 ; // i 和 j 表示此时填的位置。
while( x < n ){
// 按照要求,先是从左往右填,所以是 i 保持不变 j 和 x 每次自增 1
/*
划重点,j < col 容易,因为最多也只有 col 列,为啥还需要 ans[i][j] == 0 呢?
因为,我们知道,如果 ans[i][j] 的值是 0 ,那么此时一定没有被前面某圈的螺旋矩阵填入数据,
反之,则这个位置已经被写入数据了,所以就不能继续此次赋值操作了,因为会覆盖以前的数据。
*/
while( j < col && ans[i][j] == 0)
ans[i][j++] = array[x++] ;
// 将需要的位置 拉回到下一起点。
j -- ;
i ++ ;
// 右边 从上往下填, j 不变, i 和 x 自增
while( i < row && ans[i][j] == 0)
ans[i++][j] = array[x++] ;
// 将需要的位置 拉回到下一起点。
j -- ;
i -- ;
// 下边 从右往左填, i 不变, j 自减 , x 自增
while( i < row && ans[i][j] == 0)
ans[i][j--] = array[x++] ;
// 将需要的位置 拉回到下一起点。
j ++ ;
i -- ;
// 左边 从下往上填, j 不变, i 自减 , x 自增
while( i < row && ans[i][j] == 0)
ans[i--][j] = array[x++] ;
// 将需要的位置 拉回到下一起点。
j ++ ;
i ++ ;
}
// 4、螺旋矩阵的输出,二维矩阵的输出,那就不是问题了
for(int i = 0 ; i < r ; i++){
cout << ans[i][0] ;
for(int j = 1 ; j < c ; j++){
cout<<" "<<ans[i][j] ;
}
cout<<endl ;
}
完整代码:
#include<bits/stdc++.h>
using namespace std ;
int ans[10010][110]={0} , num[100010] ;
int n ;
bool cmp(int a , int b){
return a > b ;
}
int main(){
cin >> n ;
for(int i = 0 ; i < n ; i++){
cin >> num[i] ;
}
sort(num , num + n , cmp) ;
int n0 = sqrt(n) , r = 1 , c , x = 0 ;// r为行,c为列
for(int i = n0 ; i >= 1 ; i--){
if(n % i == 0){
r = i ;
break ;
}
}
c = n / r ;
if(r < c) swap(r , c);
int i = 0 , j = 0 ;
while(x < n){
// 从左往右填
while( j < c && ans[i][j] == 0)
ans[i][j++] = num[x++] ;
j-- ;//调整下一条边的起点
i++ ;
// 右边从上往下填
while( i < r && ans[i][j] == 0)
ans[i++][j] = num[x++] ;
i-- ;//调整下一条边的起点
j-- ;
// 下边从右往左填
while( j >= 0 && ans[i][j] == 0)
ans[i][j--] = num[x++] ;
i-- ;//调整下一条边的起点
j++ ;
//从下往上填
while(i >= 0 && ans[i][j] == 0)
ans[i--][j] = num[x++] ;
i++ ;//调整为下一回合起点
j++ ;
}
for(int i = 0 ; i < r ; i++){
cout << ans[i][0] ;
for(int j = 1 ; j < c ; j++){
cout<<" "<<ans[i][j] ;
}
cout<<endl ;
}
return 0 ;
}
PAT (Basic Level) Practice (中文) 1050 螺旋矩阵 (25 分) 凌宸1642的更多相关文章
- PAT (Advanced Level) Practice 1027 Colors in Mars (20 分) 凌宸1642
PAT (Advanced Level) Practice 1027 Colors in Mars (20 分) 凌宸1642 题目描述: People in Mars represent the c ...
- PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) 凌宸1642
PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) 凌宸1642 题目描述: A number that will ...
- PAT (Advanced Level) Practice 1011 World Cup Betting (20 分) 凌宸1642
PAT (Advanced Level) Practice 1011 World Cup Betting (20 分) 凌宸1642 题目描述: With the 2010 FIFA World Cu ...
- PAT (Advanced Level) Practice 1005 Spell It Right (20 分) 凌宸1642
PAT (Advanced Level) Practice 1005 Spell It Right (20 分) 凌宸1642 题目描述: Given a non-negative integer N ...
- PAT (Advanced Level) Practice 1001 A+B Format (20 分) 凌宸1642
PAT (Advanced Level) Practice 1001 A+B Format (20 分) 凌宸1642 题目描述: Calculate a+b and output the sum i ...
- PAT (Basic Level) Practice (中文)1055 集体照 (25 分) 凌宸1642
PAT (Basic Level) Practice (中文)1055 集体照 (25 分) 凌宸1642 题目描述: 拍集体照时队形很重要,这里对给定的 N 个人 K 排的队形设计排队规则如下: 每 ...
- PAT (Basic Level) Practice (中文)1070 结绳 (25 分) 凌宸1642
PAT (Basic Level) Practice (中文)1070 结绳 (25 分) 凌宸1642 题目描述 给定一段一段的绳子,你需要把它们串成一条绳.每次串连的时候,是把两段绳子对折,再如下 ...
- PAT (Basic Level) Practice (中文)1065 单身狗 (25 分) 凌宸1642
PAT (Basic Level) Practice (中文)1065 单身狗 (25 分) 凌宸1642 题目描述: "单身狗"是中文对于单身人士的一种爱称.本题请你从上万人的大 ...
- PAT (Advanced Level) Practice 1006 Sign In and Sign Out (25 分) 凌宸1642
PAT (Advanced Level) Practice 1006 Sign In and Sign Out (25 分) 凌宸1642 题目描述: At the beginning of ever ...
随机推荐
- 如何使用 js 实现一个 throttle 函数
如何使用 js 实现一个 throttle 函数 原理 实现方式 "use strict"; /** * * @author xgqfrms * @license MIT * @c ...
- HTML5 download 执行条件
HTML5 download 执行条件 同一个域名下的资源 http only 绝对路径/相对路径 都可以 demo https://cdn.xgqfrms.xyz/ https://cdn.xgqf ...
- Python Coding Interview
Python Coding Interview Python Advanced Use enumerate() to iterate over both indices and values Debu ...
- js uppercase the first letter of string
js uppercase the first letter of string js String.toUpperCase `-webkit-border-image`.split(`-`).filt ...
- DOM & Node.contains
DOM & Node.contains Node.contains() https://developer.mozilla.org/en-US/docs/Web/API/Node/contai ...
- CSS border gradient color All In One
CSS border gradient color All In One CSS Gradient Borders border-image-source & border-image-sli ...
- docs search & algolia & docsearch
docs search & algolia & docsearch https://www.algolia.com/docsearch https://www.algolia.com/ ...
- nodejs ECMAScript 模块
node 文档 main.mjs import path from 'path'; import logger from './logger.js'; logger.show('hello world ...
- 应该如何看待VAST的未来价格与价值?
提起数字货币的价格,很多币圈人士都是滔滔不绝,随口一举例,便是百倍千倍的数字货币.可是提起数字货币的价值,就很少有币圈人士能举出几个有力的例子,常常顾左右而言他,场面十分尴尬.之所以会这样,是因为很多 ...
- USDN代币多少钱?USDN有什么用?
加密货币走向主流人群的采用有很多障碍,比如监管.交易所黑客事件等,但最明显的障碍还是它们极端的价格波动.这从加密货币的整个历史长度来看都是如此.一个货币要正常运转,比如成为有效的交换媒介.记账单位以及 ...