当前位置:网站首页>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;
}
边栏推荐
- 756. 蛇形矩阵
- Error: cannot find module 'UMI' problem handling
- Database operation skills 7
- Where are the laravel framework log files stored? How to use it?
- The idea shortcut key ALT realizes the whole column operation
- Database operation topic 1
- Pat grade a a1076 forwards on Weibo
- Web overview and b/s architecture
- 209. Subarray with the smallest length
- unity TopDown角色移动控制
猜你喜欢
Pop up window in Win 11 opens with a new tab ---firefox
2022流动式起重机司机考试题模拟考试题库模拟考试平台操作
Uploading pictures on Alibaba cloud OSS
The child and binary tree- open root inversion of polynomials
十大蓝筹NFT近半年数据横向对比
Babbitt | metauniverse daily must read: does the future of metauniverse belong to large technology companies or to the decentralized Web3 world
redis原理和使用-安装和分布式配置
Node-v download and application, ES6 module import and export
Horizontal comparison of the data of the top ten blue chip NFTs in the past half year
Introduction to excellent verilog/fpga open source project (30) - brute force MD5
随机推荐
How to quickly learn a programming language
Introduction to excellent verilog/fpga open source project (30) - brute force MD5
NPM add source and switch source
2022化工自动化控制仪表操作证考试题模拟考试平台操作
【无标题】
at、crontab
(1) CTS tradefed test framework environment construction
Uploading pictures on Alibaba cloud OSS
Pytoch realizes logistic regression
Nuxt - 项目打包部署及上线到服务器流程(SSR 服务端渲染)
李沐d2l(六)---模型选择
语音聊天app源码——钠斯直播系统源码
JVM触发minor gc的条件
Study notes of dataX
对象型的集合按某个属性的值进行去重
聪明的美食家 C语言
[leetcode database 1050] actors and directors who have cooperated at least three times (simple question)
"Could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32"
Center an element horizontally and vertically
2022茶艺师(中级)特种作业证考试题库模拟考试平台操作