当前位置:网站首页>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;
}
边栏推荐
- Innovus卡住,提示X Error:
- 公告 | FISCO BCOS v3.0-rc4发布,新增Max版,可支撑海量交易上链
- Unity topdown character movement control
- Pytoch realizes logistic regression
- Li Mu D2L (VI) -- model selection
- 2022流动式起重机司机考试题模拟考试题库模拟考试平台操作
- Which of count (*), count (primary key ID), count (field) and count (1) in MySQL is more efficient? "Suggested collection"
- Self review ideas of probability theory
- 【ARKit、RealityKit】把图片转为3D模型
- Review notes of Microcomputer Principles -- zoufengxing
猜你喜欢
随机推荐
Learn more about the difference between B-tree and b+tree
“No input file specified “问题的处理
PHP 之 Apple生成和验证令牌
PHP和MySQL获取week值不一致的处理
深度学习常用激活函数总结
《Datawhale熊猫书》出版了!
Datawhale panda book has been published!
Processing of inconsistent week values obtained by PHP and MySQL
2B和2C
数据库操作技能7
JS - DataTables 关于每页显示数的控制
838. 堆排序
垂直搜索
Center an element horizontally and vertically
Pop up window in Win 11 opens with a new tab ---firefox
[eslint] Failed to load parser ‘@typescript-eslint/parser‘ declared in ‘package. json » eslint-confi
Zipkin安装和使用
JS closure: binding of functions to their lexical environment
力扣——二叉树剪枝
NFT与数字藏品到底有何区别?









