当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
随机推荐
Laravel框架日志文件存放在哪里?怎么用?
Replication of SQL injection vulnerability in the foreground of Pan micro e-cology8
Store a group of positive and negative numbers respectively, and count the number of 0 -- assembly language implementation
数据库操作技能7
李沐d2l(五)---多层感知机
Learning notes of automatic control principle --- linear discrete system
Database operation topic 2
十大蓝筹NFT近半年数据横向对比
围棋智能机器人阿法狗,阿尔法狗机器人围棋
Pytoch realizes logistic regression
Zipkin安装和使用
Numpy Foundation
Database operation topic 1
day06 作业--技能题1
Kotlin properties and fields
What is the difference between NFT and digital collections?
2022茶艺师(中级)特种作业证考试题库模拟考试平台操作
PAT 甲级 A1076 Forwards on Weibo
“No input file specified “问题的处理
聪明的美食家 C语言


![[leetcode database 1050] actors and directors who have cooperated at least three times (simple question)](/img/ab/bad6b86039384af7b829bef5316923.png)






