当前位置:网站首页>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;
}
边栏推荐
- 请问同一个oracle cdc表,如果flink job重新提交,是会全量读取一遍源数据还是增量呢?
- MindSpore:mirrorpad算子速度过慢的问题
- Detailed explanation of MSTP protocol configuration on Layer 3 switches [Huawei eNSP experiment]
- Anton Paar安东帕密度计比重计维修DMA35性能参数
- 反序列化漏洞
- Fiddler(一)安装
- RL学习笔记(一)
- 如何快速将Zabbix5.0升级至6.0?
- [Punctuality Atom STM32 Serial] Chapter 2 STM32 Introduction Excerpted from [Punctual Atom] MiniPro STM32H750 Development Guide_V1.1
- oracle sql multi-table query
猜你喜欢

冰蝎逆向初探

Producer and Consumer Problems in Concurrent Programming

请你谈谈网站是如何进行访问的?【web领域面试题】

抬升市场投资情绪,若羽臣是否还需“自身硬”?

Shell编程的条件语句

我和 TiDB 的故事 | TiDB 对我不离不弃,我亦如此

思想茶叶蛋 (Jul 31,2022)| 元宇宙(Metaverse)下了一枚什么样的蛋

ISO14443A读卡流程(作为示例参考)

已解决No module named ‘flask_misaka‘【BUG解决】

VRRP + MSTP configuration, huawei eNSP experiment 】 【
随机推荐
蘑菇书EasyRL学习笔记
leetcode二叉树系列(一)
yuv420sp转jpg
Producer and Consumer Problems in Concurrent Programming
grafana手册之可视化配置图表table
今日睡眠质量记录71分
[Punctuality Atom STM32 Serial] Chapter 2 STM32 Introduction Excerpted from [Punctual Atom] MiniPro STM32H750 Development Guide_V1.1
RL学习笔记(一)
浅聊偏函数
请你谈谈网站是如何进行访问的?【web领域面试题】
四大网络攻击常见手段及防护
【正点原子STM32连载】第二章 STM32简介 摘自【正点原子】MiniPro STM32H750 开发指南_V1.1
【C补充】指向指针或函数的指针
MindSpore:mirrorpad算子速度过慢的问题
cannot import name 'import_string' from 'werkzeug' [bug solution]
下午14:00面试,14:08低着头出来了 ,问的实在是太...
ps如何换背景颜色,自学ps软件photoshop2022,3种不同的方式笔记记录
How Oracle for current library or certain library data on the same server number?
ansible部署脚本--亲测可用无坑
2022年制冷与空调设备运行操作特种作业证考试题库及模拟考试