当前位置:网站首页>756. 蛇形矩阵
756. 蛇形矩阵
2022-07-26 09:05:00 【Hunter_Kevin】
题目
输入两个整数 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
代码
#include <iostream>
using namespace std;
int q[110][110];
int main()
{
int n, m;
cin >> n >> m;
int dx[] = {
-1,0,1,0}, dy[] = {
0,1,0,-1};
int d = 1;
int x = 0, y = 0;
for(int i = 1; i <= n*m; i++)
{
q[x][y] = i;
int ne_x = x + dx[d], ne_y = y + dy[d];
if(ne_x < 0 || ne_x >= n || ne_y < 0 || ne_y >= m || q[ne_x][ne_y])
{
d = (d+1) % 4;
ne_x = x+dx[d], ne_y = y+dy[d];
}
x = ne_x, y = ne_y;
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
printf("%d ",q[i][j]);
printf("\n");
}
return 0;
}
边栏推荐
- day06 作业--技能题2
- The largest number of statistical absolute values --- assembly language
- What is the difference between NFT and digital collections?
- Rocky基础练习题-shell脚本2
- Cve-2021-21975 VMware SSRF vulnerability recurrence
- Replication of SQL injection vulnerability in the foreground of Pan micro e-cology8
- 数据库操作 题目一
- 《Datawhale熊猫书》出版了!
- JDBC数据库连接池(Druid技术)
- 公告 | FISCO BCOS v3.0-rc4发布,新增Max版,可支撑海量交易上链
猜你喜欢

(2006,Mysql Server has gone away)问题处理

The idea shortcut key ALT realizes the whole column operation

Vision Group Training Day5 - machine learning, image recognition project

Unity topdown character movement control

209. Subarray with the smallest length

Day 6 summary & database operation

深度学习常用激活函数总结

2022化工自动化控制仪表操作证考试题模拟考试平台操作

Canal 的学习笔记

CSDN Top1 "how does a Virgo procedural ape" become a blogger with millions of fans through writing?
随机推荐
(1) CTS tradefed test framework environment construction
JS - DataTables 关于每页显示数的控制
JDBC database connection pool (Druid Technology)
redis原理和使用-安装和分布式配置
Simple message mechanism of unity
多项式开根
Day06 operation -- addition, deletion, modification and query
Day 6 summary & database operation
Cve-2021-21975 VMware SSRF vulnerability recurrence
Form form
day06 作业--技能题2
数据库操作 题目一
Day06 homework -- skill question 1
Clean the label folder
CF1481C Fence Painting
Laravel框架日志文件存放在哪里?怎么用?
Uni app simple mall production
Two tips for pycharm to open multiple projects
Pop up window in Win 11 opens with a new tab ---firefox
Web概述和B/S架构