当前位置:网站首页>756. Serpentine matrix
756. Serpentine matrix
2022-07-26 09:11:00 【Hunter_ Kevin】
subject
Enter two integers n and m, Output one n That's ok m Columns of the matrix , The digital 1 To n×m Fill the matrix in the shape of a serpentine .
For the specific matrix form, please refer to the example .
Input format
Enter a total of one line , Contains two integers n and m.
Output format
Output a matrix that meets the requirements .
Matrix occupation n That's ok , Each row contains m An integer separated by spaces .
Data range
1≤n,m≤100
sample input :
3 3
sample output :
1 2 3
8 9 4
7 6 5
Code
#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;
}
边栏推荐
猜你喜欢

JDBC database connection pool (Druid Technology)

Datax的学习笔记

ext3文件系统的一个目录下,无法创建子文件夹,但可以创建文件

Qtcreator reports an error: you need to set an executable in the custom run configuration

Datawhale panda book has been published!

李沐d2l(四)---Softmax回归

220. Presence of repeating element III

Database operation skills 6

2022流动式起重机司机考试题模拟考试题库模拟考试平台操作

Learning notes of automatic control principle - Performance Analysis of continuous time system
随机推荐
Day06 homework - skill question 7
Canal 的学习笔记
js闭包:函数和其词法环境的绑定
高数 | 武爷『经典系列』每日一题思路及易错点总结
tornado之多进程服务
The idea shortcut key ALT realizes the whole column operation
对象型的集合按某个属性的值进行去重
网络安全漫山遍野的高大上名词之后的攻防策略本质
数据库操作技能7
220. Presence of repeating element III
Innovus卡住,提示X Error:
Pat grade a a1013 battle over cities
Datawhale panda book has been published!
Horizontal comparison of the data of the top ten blue chip NFTs in the past half year
谷粒学院的全部学习源码
NFT与数字藏品到底有何区别?
优秀的 Verilog/FPGA开源项目介绍(三十零)- 暴力破解MD5
Nuxt - 项目打包部署及上线到服务器流程(SSR 服务端渲染)
【无标题】
redis原理和使用-基本特性