当前位置:网站首页>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;
}
边栏推荐
- Espressif plays with the compilation environment
- 2022茶艺师(中级)特种作业证考试题库模拟考试平台操作
- The Child and Binary Tree-多项式开根求逆
- Processing of inconsistent week values obtained by PHP and MySQL
- CSDN Top1 "how does a Virgo procedural ape" become a blogger with millions of fans through writing?
- SQL入门——组合表
- Elastic APM安装和使用
- 对象型的集合按某个属性的值进行去重
- PAT 甲级 A1013 Battle Over Cities
- Nuxt - Project packaging deployment and online to server process (SSR server rendering)
猜你喜欢
随机推荐
The Child and Binary Tree-多项式开根求逆
NPM add source and switch source
Study notes of automatic control principle -- correction and synthesis of automatic control system
[recommended collection] MySQL 30000 word essence summary - query and transaction (III)
2022流动式起重机司机考试题模拟考试题库模拟考试平台操作
redis原理和使用-安装和分布式配置
Learn more about the difference between B-tree and b+tree
巴比特 | 元宇宙每日必读:元宇宙的未来是属于大型科技公司,还是属于分散的Web3世界?...
zsh: command not found: nvm
论文笔记: 知识图谱 KGAT (未完暂存)
CSDN TOP1“一个处女座的程序猿“如何通过写作成为百万粉丝博主?
【无标题】
mysql函数
服务器内存故障预测居然可以这样做!
2022年上海市安全员C证考试试题及模拟考试
JS - DataTables 关于每页显示数的控制
Grain College of all learning source code
187. Repeated DNA sequence
ext4文件系统打开了DIR_NLINK特性后,link_count超过65000的后使用link_count=1来表示数量不可知
Matlab 绘制阴影误差图









