当前位置:网站首页>Basic practice of Blue Bridge Cup - shape retrieval of matrix (C language)
Basic practice of Blue Bridge Cup - shape retrieval of matrix (C language)
2022-07-25 19:26:00 【Aricl.】
Catalog
One 、 Problem description :
To take a number from a loop is to take a number along the edge of a matrix , If the current direction of countless or have taken , Turn left 90 degree . It starts at the top left corner of the matrix , Direction down .
Input format :
The first line of input is no more than 200 The positive integer m, n, The rows and columns that represent the matrix . Next m Every line n It's an integer , To represent this matrix .
Output format :
There is only one line of output , common mn Number , The result of taking data for the input matrix . Numbers are separated by a space , There should be no extra space at the beginning and end of the line .
The sample input 1:
3 3
1 2 3
4 5 6
7 8 9
Sample output 1:
1 4 7 8 9 6 3 2 5
The sample input 2:
3 2
1 2
3 4
5 6
The sample input 2:
1 3 5 6 4 2
Two 、 Their thinking
This topic examines two-dimensional arrays and circular structures , It belongs to the basic topic . The topic mainly stipulates a rule of data retrieval , That is, counter clockwise, take data in turn according to the shape and output . Because there are many kinds of matrix shapes , It can be roughly divided into two kinds :1. A matrix with equal rows and columns 2. Matrix with unequal rows and columns . And it should be considered that some small matrices may be completed in less than one cycle , Such as a matrix with three rows and two columns , Just finish down , towards the right , Up again, the data retrieval is completed , There is no complete circle . The complete circle should be : Next —> Right —> On —> Left
So the code should look at the problem in a general way , That is, get the general solution of the problem instead of the special solution . In the following code, each step of the operation is while In the circulatory system &&count Its function is to judge whether all the numbers have been taken , Whether to perform the next step of data retrieval .
Because the comments in the code are already very detailed , No more details here .
3、 ... and 、 Code
// Based on practice : Return to the shape of the data
#include<stdio.h>
int main(){
int m,n,count;
int i,j;
// Parameter description :m For the line n Column ,count Is the total number of matrix elements
scanf("%d%d",&m,&n);
count=m*n;
int arr[m][n];
// Next, initialize the matrix
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&arr[i][j]);
// Now let's start the operation of shape retrieval : Down , towards the right , Up , towards the left ...... Cyclic repetition
i=0;j=0;// there i,j Respectively represent the coordinate positions of elements in the matrix (i,j)
int circle=0;//circle Represents the number of cycles of loop retrieval
while(count){
// Take the number down : The line is changing , The column does not change
while(i<m-circle&&count){
if(i==0&&j==0){
printf("%d",arr[0][0]);
i++;// Easily overlooked points : Print out the first element in the upper left corner , Be sure to move the line to the next line !
}
else
printf(" %d",arr[i++][j]);
count--;// Number of each print , Make total count Minus one
}
i--;// Because after printing the last row element of the column ,i Or automatically add 1, It will be crossed ,
// so i-- Bring it back to the circle The last line of circle fetching down
j++;// After the downward data retrieval , Add one to the number of columns , Ready to fetch right
while(j<n-circle&&count){
printf(" %d",arr[i][j++]);
count--;
}
j--;// Empathy
i--;// Prepare to fetch up
while(i>=circle&&count){
printf(" %d",arr[i--][j]);
count--;
}
i++;// Empathy
j--;// Ready to fetch left
while(j>circle&&count){
printf(" %d",arr[i][j--]);
count--;
}
j++;// Empathy
i++;// The loop takes one round and ends , The next loop of loop retrieval starts from the second element on the right diagonal ......
circle++;// The lap ends , Turns plus one
}
return 0;
} Running results :



If you feel something after reading it , Click a like or praise, and then go ~

边栏推荐
- GBASE 8s UDR内存管理_02_mi_dalloc
- Sccm2012r2 network deployment reinstallation system
- Telnet installation and telnet (correct password) cannot log in!
- AAAI 2022 | GAN的结构有“指纹”吗?从伪造图像溯源生成网络结构
- 【DETR用于3D目标检测】3DETR: An End-to-End Transformer Model for 3D Object Detection
- Clip can also do segmentation tasks? The University of Gottingen proposed a model clipseg that uses text and image prompt and can do three segmentation tasks at the same time, squeezing out the clip a
- 微信小程序 28 热搜榜的完善①
- 高效生成接口文档好方法
- 600000 pieces of data are made from March 1 to March 31. Videodate requires starting time from 00:00 to 24:00 on March 1 to 31, which is only for notes
- [Detr for 3D object detection] detr3d: 3D object detection from multi view images via 3D-to-2D queries
猜你喜欢

What is the application value of MES management system

帝国CMS整站|手机号/QQ靓号商城源码|适配移动端

微信小程序 28 热搜榜的完善①

Flutter tips: optimizing the buildcontext you use

小程序毕设作品之微信校园维修报修小程序毕业设计成品(6)开题答辩PPT

Eve - 0day Threat Intelligence

The finished product of wechat campus maintenance and repair applet graduation design (1) development outline

微信小程序 27 进度条的动态实现和搜索框、热搜榜的静态搭建

Pymoo learning (5): convergence analysis

网上商城系统MySql数据库设计项目实战
随机推荐
北航等《深度学习事件抽取》文献综述论文,27页pdf阐述当前趋势
TFIDF examples and explanations
小程序毕设作品之微信校园维修报修小程序毕业设计成品(8)毕业设计论文模板
Introduction to web security ICMP testing and defense
Old wine in new bottles -- sample analysis of recent apt32 (sea Lotus) organizational attacks
微信小程序 29 热搜榜的完善②
小程序毕设作品之微信校园维修报修小程序毕业设计成品(7)中期检查报告
Wechat campus maintenance application applet graduation design finished product of applet completion work (8) graduation design thesis template
Wechat campus maintenance and repair applet graduation design finished product (7) Interim inspection report
小程序毕设作品之微信校园维修报修小程序毕业设计成品(5)任务书
Small program completion work wechat campus maintenance application small program graduation design finished product (2) small program function
解决Win10账户没有了管理员权限
加州大学|用于未指定环境的可行对抗鲁棒强化学习
Wechat campus maintenance and repair applet graduation design finished product of applet completion work (4) opening report
【小程序开发】宿主环境详解
[applet development] common components and basic usage details
微信小程序开发之网络数据请求
GBASE 8s UDR内存管理_02_mi_dalloc
Wechat campus maintenance and repair applet graduation design finished product (5) assignment of applet completion work
Actual combat of MySQL database design project of online mall system