1105 Spiral Matrix (25 point(s))

现代软件开发技术 | 第1章:Spring基础 Spring是一个轻量级Java 开发框架,最早由Rod Johnson创建,目的是为了解决企业级应用开发的业务逻辑层和其他各层的耦合问题。 阅读详情

1105 Spiral Matrix (25 point(s))

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×n must be equal to N; m≥n; and m−nis 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

模拟题。元素的规律填充。

思路:

1. 先确定行数、列数;

2. 考虑填充过程。对于给定的范围(M*N),用i和j表示坐标,用dirIndex方向的控制指标,从(0,0)开始,依次向右、下、左、上移动。对于每一步确定下一个元素的坐标,如果按照当前方向,新的坐标会超出给定范围或者该元素已经有了元素,则变换方向,否则保持当前方向。直到所有元素填充完。

3. 按照格式输出。 

#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX = 1e4+7;
int n[MAX];
bool cmp(int a,int b){return a>b;}
int main(void){
	int N;cin>>N;
	for(int i=0;i<N;i++) cin>>n[i];
	sort(n,n+N,cmp);//排序 
	int col = sqrt(N);//确定这个矩形的高和宽 
	do{
		if(N%col==0) break;
	}while(col--);
	int row = N/col;
	int a[row][col];
	memset(a,0,sizeof(a));
	int i=0,j=-1;//初始位置 
	int dirIndex = 0;
	int dir[4][2]={0,1, 1,0, 0,-1, -1,0};
	for(int k=0;k<N;k++){
		int ii = i+dir[dirIndex%4][0];
		int jj = j+dir[dirIndex%4][1];
		if(ii<0||ii>=row||jj<0||jj>=col||a[ii][jj]!=0){
		//如果继续维持当前方向发生超出范围 
			dirIndex++;//变更方向 
		} 
		i = i+dir[dirIndex%4][0];
		j = j+dir[dirIndex%4][1];
		a[i][j]=n[k];
	}
	for(int i=0;i<row;i++){
		for(int j=0;j<col;j++){
			if(j==0) cout<<a[i][j];
			else cout<<" "<<a[i][j];
		}
		cout<<endl;
	}
	return 0;
}

 

相关推荐

别再只盯着Text-VQA了!这4个主流VQA数据集(含中英双语)的保姆级下载与对比指南

本文深入解析了Text-VQA、ST-VQA、OCR-VQA和EST-VQA这4个主流视觉问答数据集的特性与应用场景,提供了详细的下载与预处理指南。通过对比分析,帮助开发者根据项目需求选择最适合的数据集,避免因选型不当导致的模型性能问题,特别适合需要处理中英双语或多模态文本理解的AI项目。

weixin_42524708的博客 362

1105 Spiral Matrix (25point(s)) - C语言 PAT 甲级

设计思路: 总体设计方向是一层一层填充 m x n 的矩阵 1.确定 m 和 n 的值,n 从 sqrt(N) 趋于 0,第一个 N % n == 0 成立即找到 2.确定矩阵有几层,level = n / 2 + n % 2 3.每一层有四个方向 注意:如何保存矩阵?一种方法是直接利用一维数组模拟二维,但操作相对繁琐,本文采用变长数组进行保存,较为方便。不要预设矩阵为 10000 x 10000...

有三只土豆 186

单总线CPU设计(变长指令周期3级时序)(HUST)(头歌实验)

利用比较器等功能模块将32位MIPS 指令字译码生成LW、SW、BEQ、SLT、ADDI、OtherInstr信号也就是利用比较器将指令字转换为译码信号,OP与不同信号对应(查询MIPS手册得知) 16 进制 :23 是 2进制: 00100011 ,把获得的OP,Func,和常数比对,相同输出1.按照题目要求填写表格,使用logisim的组合逻辑电路分析功能,当然也可以直接再logisim中写真值表。单总线采用变长指令周期,不同指令周期数不同,节拍数可变化。写的excel表格最终生成电路(当然 需要先把

xuxuaifeifei的博客 1万+

1105 Spiral Matrix (25 point(s))

题解 蛇形数组。 #include&lt;iostream&gt; #include&lt;vector&gt; #include&lt;cstdio&gt; #include&lt;cmath&gt; #include&lt;algorithm&gt; using namespace std; int n, m, N; vector&lt;int&gt; t; int main() ...

徐伯莱 296

AD域控设置客户端桌面壁纸

此时再右键wallpaper文件夹,共享选项下的网络路径已经有了。找到该域下的组策略编辑器(第一步有)并按照图下所示找到桌面壁纸的策略设置,点击进去。在高级共享里可以设置共享名和同时共享的用户数量限制。点击下拉箭头,找到Everyone并选择,点击添加后,点击共享。然后可以看到刚才的新建文件夹策略在域控服务器和客户端都生效了。右键wallpaper文件夹->属性,点击共享选项,点击共享。在弹出的窗口里,按照图下所示设置,点击应用并且确认。在弹出的窗口里,按图示找到文件夹,右键新建文件夹。

欲买桂花同载酒的博客 6056

智慧楼宇解决方案-全套大合集

智慧楼宇解决方案,楼宇能耗-环境-设备统一管控解决方案。

byte58的博客 2592

【PAT】A1105 Spiral Matrix (25 point(s))

文章目录A1105 Spiral Matrix (25 point(s))Input Specification:Output Specification:Sample Input:Sample Output: A1105 Spiral Matrix (25 point(s)) This time your job is to fill a sequence of N positive integ...

Duke 204

Marvell车载以太网交换机芯片88Q5050

Marvell推出了四款车载用交换机芯片,88Q5050,88Q5050, 88Q5072和88Q6113。其中88Q5030有5 Port用于通信,88Q5050有8 Port用于通信,5072与6113有11 Port用于通信。由于项目中用到了88Q5050,所以本文中只涉及到88Q5050的内容。本文是对使用88Q5050的梳理和总结。

zhuwade的博客 2万+

PTA甲级 1105 Spiral Matrix (25point(s))

强烈推荐,刷PTA的朋友都认识一下柳神–PTA解法大佬 本文由参考于柳神博客写成 柳神的优快云博客,这个可以搜索文章 柳神的个人博客,这个没有广告,但是不能搜索 还有就是非常非常有用的 算法笔记 全名是 算法笔记 上级训练实战指南 //这本都是PTA的题解 算法笔记 PS 今天也要加油鸭 题目原文 This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasin

Huangpengyu123的博客 318

通过python+openCV实现对图片中箭头方向的判断

摘要: 针对YOLOv8算法在箭头方向识别效果不佳的问题,提出了一种基于OpenCV的解决方案。该方法通过图像预处理、轮廓分析、几何特征提取(凸包、质心、外接矩形)和方向向量计算,准确判断箭头的上下左右方向。代码示例展示了从图像读取到方向识别的完整流程,并提供了优化建议(如调整二值化阈值、处理多轮廓)。该方案在Windows11+Python3.12+OpenCV环境下验证有效,适用于需要简单几何特征分析的箭头方向检测场景。

AnChenliang_1002的博客 703

《SOC架构-ARM架构-文档导读》-目录

小白请看“Armv8/Armv9架构从入门到精通”专栏。以翻译为主的,深度深度深度深度/深入深入深入 学习ARM架构的文章。

baron-周贺贺-代码改变世界ctw 2085

PAT_甲级_1105 Spiral Matrix (25point(s)) (C++)【螺旋矩阵】

1,题目描述 spiral:螺旋; 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 2,思路 3,AC代码 4,解题过程 ...

&再见萤火虫&的博客 308

结构光相移法-多频外差原理与实践

结构光相移法是一种常用的三维形貌测量技术,它通过对投射到目标物体上的结构光进行相位调制和解调,实现对物体表面的三维重建。其中,多频外差原理是结构光相移法的核心原理之一,通过使用不同频率的结构光来实现相位测量,并可以提高测量精度和抗干扰性能。结构光相移法的多频外差原理是一种常用的三维形貌测量技术,它通过使用多个不同频率的结构光进行相位调制和解调,实现对物体表面的三维重建。本文介绍了多频外差原理的原理和实践应用,并提供了相应的Python源代码,希望对读者理解和应用结构光相移法有所帮助。

CodeByte的博客 737

PAT 甲级 1105  Spiral Matrix

1105 Spiral Matrix (25 point(s)) 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 t...

漫浸天空的雨色的备忘录~ 874

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

【PAT】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

此去经年 1790

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

甲级1105 Spiral Matrix (25 分)

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

weixin_44751612的博客 369

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

pta甲级 1105 Spiral Matrix (25分)

链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805363117768704 题意:给你N个数,将其放入非递增的螺旋矩阵中,矩阵大小为m*n,要求: 1)N = m * n 2) m >= n 3) m-n最小 思路:按照走路+转向的思路,当碰壁(超出矩阵范围或已有值)就顺时针转向。 #include <bits/stdc++.h> #define ll long long using nam

birdmanqin的博客 197

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

ysq96的博客 322

PAT甲级 1105 Spiral Matrix (25 分)模拟

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

qq_29762941的博客 268

1105 Spiral Matrix(25 分)(cj)

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

Cute_jinx的博客 317

1105 Spiral Matrix (25 分)

This time your job is to fill a sequence ofNpositive integers into aspiral matrixin non-increasing order. A spiral matrix is filled in from the first element at the upper-left corner, then move in...

张蔡辉 358

B1105 Spiral Matrix (画图)

B1105 Spiral Matrix (25分) //第一次只拿了21分 矩阵的长和宽,求最大因子,从sqrt(num)开始枚举. 每次循环一次,s++,t--,d--,r++ 测试点四运行超时,是因为输入一个数字的时候,需要直接输出这个数字。//1分 测试点二运行超时,最后一个数字不必再while循环一次,直接输出即可。//3分 最后一个测试点卡了好久/(ㄒoㄒ)/~~...

没有期待的日子反而会顺顺利利 256

PAT 1105 Spiral Matrix (25分)

题目描述 题目链接 代码 #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <vector> #include <algorithm> #define MAX 10010 using namespace std...

ChasingTheFreeWind 159

1105 Spiral Matrix (25 分)

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

galesaur_wcy 296

1105 Spiral Matrix

1105 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 matrix is filled in from the first element at the upper-left ...

ssf_cxdm的博客 160

PAT1105【Spiral Matrix】 简单模拟

先这个博客的目的是提醒自己大数组初始化用memset或者fill,直接在定义的时候初始化很容易出问题。#include&lt;bits/stdc++.h&gt; using namespace std; const int maxn = 1e4+10; int N,n,dmin=1e9; int arr[maxn]; int show[500][500]; bool cmp(int a, int ...

gq__97的博客 555
上一篇: 1102 Invert a Binary Tree (25 point(s))
下一篇: 1128 N Queens Puzzle (20 point(s))
coderwait
博客等级 码龄9年 20粉丝 210原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值