当前位置:网站首页>LeetCode 54. 螺旋矩阵 蛇形矩阵式输出字符串
LeetCode 54. 螺旋矩阵 蛇形矩阵式输出字符串
2022-08-04 09:23:00 【超级码力奥】
输入两个整数 n 和 m,输出一个 n 行 m 列的矩阵,将数字 1 到 n×m 按照回字蛇形填充至矩阵中。
具体矩阵形式可参考样例。
输入格式
输入共一行,包含两个整数 n 和 m。
输出格式
输出满足要求的矩阵。
矩阵占 n 行,每行包含 m 个空格隔开的整数。
数据范围
1≤n,m≤100
输入样例:
3 3
输出样例:
1 2 3
8 9 4
7 6 5
原题连接:
https://www.acwing.com/problem/content/758/
// 太优雅了,简直太优雅了。。
// 利用left, right, top, bottom四个变量,来表示矩形的四个边界
// 抽象成每次循环处理一个环
#include<iostream>
using namespace std;
const int N = 105;
int a[N][N];
int n, m;
int main()
{
cin >> n >> m;
int left = 0, right = m - 1, top = 0, bottom = n - 1;
int k = 1;
while(left <= right && top <= bottom)
{
// 处理上部
for(int i = left; i <= right; i ++)
a[top][i] = k ++;
// 处理右部
for(int i = top + 1; i <= bottom; i ++)
a[i][right] = k ++;
// 处理下部,有可能没有下部,所以要多个条件top < bottom
for(int i = right - 1; i >= left && top < bottom; i --)
a[bottom][i] = k ++;
// 处理左部分,有可能没有左部
for(int i = bottom - 1; i > top && left < right; i --)
a[i][left] = k ++;
left ++, right --, top ++, bottom --;
}
for(int i = 0; i < n; i ++)
{
for(int j = 0; j < m; j ++)
cout << a[i][j] << " ";
cout << endl;
}
return 0;
}
边栏推荐
- Detailed Explanation of Addresses Delivered by DHCP on Routing/Layer 3 Switches [Huawei eNSP]
- [Punctuality Atom STM32 Serial] Chapter 4 STM32 First Experience Excerpted from [Punctual Atom] MiniPro STM32H750 Development Guide_V1.1
- Oracle怎么获取当前库或者同一台服务器上某几个库的数据总行数?
- 基于cRIO-904X搭建Simulink与Labview环境
- 关于DSP驱动外挂flash
- ansible部署脚本--亲测可用无坑
- 云函数实现网站自动化签到配置详解【Web函数/Nodejs/cookie】
- IDEA 自动导入的配置(Auto import)
- Win11不识别蓝牙适配器的解决方法
- 记录十条工作中便利的API小技巧
猜你喜欢

MindSpore:Batchnorm only support nchw input!

TiCDC同步延迟问题处理

字符串相关题目

Apache APISIX 2.15 版本发布,为插件增加更多灵活性

Interview at 14:00 in the afternoon, I came out at 14:08 with my head down, asking too much...

leetcode单调栈经典例题——最大矩形

MySQL binlog都有哪些模式?
![[Cloud Residency Co-Creation] HCSD Celebrity Live Streaming – Employment Guide](/img/50/86f0edaab8317e22c9ffdb2a2c6e93.png)
[Cloud Residency Co-Creation] HCSD Celebrity Live Streaming – Employment Guide

路由/三层交换机DHCP下发地址详解【华为eNSP】
![一道[CSCCTF 2019 Qual]FlaskLight的详解再遇SSTI](/img/98/8b2359b7b99da9e50821cdaaf5e47d.png)
一道[CSCCTF 2019 Qual]FlaskLight的详解再遇SSTI
随机推荐
软件工程国考总结——判断题
【C补充】指针相关知识点收集01
低代码应用开发的五大好处
The separation configuration Libpq is supported, speaking, reading and writing
TiDB升级与案例分享(TiDB v4.0.1 → v5.4.1)
关于技术学习的6个观点
[Punctuality Atom STM32 Serial] Chapter 4 STM32 First Experience Excerpted from [Punctual Atom] MiniPro STM32H750 Development Guide_V1.1
2022年制冷与空调设备运行操作特种作业证考试题库及模拟考试
Apache APISIX 2.15 版本发布,为插件增加更多灵活性
2022 Cloud Native Computing代表厂商 | 灵雀云第三次入选Gartner中国ICT技术成熟度曲线报告
开源一夏 | 查询分页不只有limit,这四种分页方法值得掌握
VRRP + MSTP configuration, huawei eNSP experiment 】 【
MindSpore:【model_zoo】【resnet】尝试用THOR优化器运行时报cannot import name ‘THOR‘
Layer 3 Switch/Router OSPF Configuration Details [Huawei eNSP Experiment]
2022-08-02 Analyze RK817 output 32k clock PMIC_32KOUT_WIFI to WiFi module clock register devm_clk_hw_register
grafana手册之可视化配置图表table
oracle sql multi-table query
Four common methods of network attacks and their protection
ZbxTable 2.0 重磅发布!6大主要优化功能!
户外徒步旅行