当前位置:网站首页>Matrix exchange row and column
Matrix exchange row and column
2022-04-23 14:25:00 【KissKernel】
## Introduce a small problem about matrix exchange rows and columns
### So let's look at the problem .
Input description :
The first line contains two integers n and m, Indicates that a matrix contains n That's ok m Column , Separate... With spaces . (1≤n≤10,1≤m≤10)
from 2 To n+1 That's ok , Enter... On each line m It's an integer ( Range -231~231-1), Separate... With spaces , Co input n*m Number , Represents the elements in the first matrix .
On the next line, type k, To perform k operations (1≤k≤5). Next there is k That's ok , Each line contains one character t And two numbers a and b, The middle is separated by an empty grid ,t Represents the action to be performed , When t Character ’r’ Time represents row transformation , When t Character ’c’ Time represents column transformation ,a and b For rows or columns that need to be interchanged (1≤a≤b≤n≤10,1≤a≤b≤m≤10).
Tips : When t For other characters, there is no need to deal with
Output description :
Output n That's ok m Column , Is the result of matrix exchange . There is a space after each number .
#### Obviously, the key to this problem is how to realize the exchange of ranks . Now let's take a look at how to exchange ranks , It's actually very simple , It's like swapping two variables .
#include<stdio.h>
int main()
{
int n,m,i,j,k;
int num1,num2;// Two rows or two columns to swap
char ch;// Decide whether to transform rows or columns
int rek;// Perform several operations
int tmp;
scanf("%d %d",&n,&m);
int arr[n][m];
// Input matrix
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",&arr[i][j]);
}
}
scanf("%d",&k);
for(rek=0;rek<k;rek++)
{
scanf(" %c %d %d",&ch,&num1,&num2);
if(ch=='r')// Perform line transformation
{
for(j=0;j<m;j++)
{
tmp=arr[num1-1][j];// When performing row transformation, agree that the rows of the matrix are invariant, cycle recursive columns, and then exchange
arr[num1-1][j]=arr[num2-1][j];
arr[num2-1][j]=tmp;
}
}
else if(ch=='c')// Perform column transformation
{
for(i=0;i<n;i++)
{
tmp=arr[i][num1-1];// Similarly, when performing column transformation, it is agreed that the two columns to be exchanged remain unchanged , Loop recursion, loop lines .
arr[i][num1-1]=arr[i][num2-1];
arr[i][num2-1]=tmp;
}
}
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
return 0;
}
Of course, if there is a simpler method, you are welcome to communicate with me. This is only one of the methods .
版权声明
本文为[KissKernel]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231412251933.html
边栏推荐
- Tongxin UOS php7 2.3 upgrade to php7.0 two point two four
- 基础正则表达式
- Five ways of using synchronized to remove clouds and fog are introduced
- 四层和八层电梯控制系统Proteus仿真设计,51单片机,附仿真和Keil C代码
- ssh限制登录的四种手段
- flannel 原理 之 TUN模式
- 剑指offer刷题(2)--面向华为
- Eight way responder system 51 Single Chip Microcomputer Design [with Proteus simulation, C program, schematic diagram, PCB files, component list and papers, etc.]
- 分分钟掌握---三目运算符(三元运算符)
- 在电视屏幕上进行debug调试
猜你喜欢

四层和八层电梯控制系统Proteus仿真设计,51单片机,附仿真和Keil C代码

数组模拟队列进阶版本——环形队列(真正意义上的排队)

API Gateway/API 网关(三) - Kong的使用 - 限流rate limiting(redis)

TLS/SSL 协议详解 (28) TLS 1.0、TLS 1.1、TLS 1.2之间的区别

C语言知识点精细详解——初识C语言【1】——你不能不知的VS2022调试技巧及代码实操【1】

Qt界面优化:Qt去边框与窗体圆角化

xx项目架构随记

51单片机的花卉、农田自动浇水灌溉系统开发,Proteus仿真,原理图和C代码

如何5分钟上手使用OCR

TLC5615 based multi-channel adjustable CNC DC regulated power supply, 51 single chip microcomputer, including proteus simulation and C code
随机推荐
Proteus simulation design of DC adjustable regulated power supply (with simulation + paper and other data)
本以为能躺着进华为,结果陆续收到京东/滴滴/爱奇艺offer的我迷茫了
TLC5615 based multi-channel adjustable CNC DC regulated power supply, 51 single chip microcomputer, including proteus simulation and C code
Four ways of SSH restricting login
Nacos uses demo as configuration center (IV)
金九银十,入职字节跳动那一天,我哭了(蘑菇街被裁,奋战7个月拿下offer)
Solve the problem of SSH configuration file optimization and slow connection
51单片机+LCD12864液晶显示的俄罗斯方块游戏,Proteus仿真、AD原理图、代码、论文等
初识STL
C语言p2选择分支语句详解
矩阵交换行列
js 进度条,显示加载进度
交通灯系统51单片机设计(附Proteus仿真、C程序、原理图及PCB、论文等全套资料)
Qt实战:云曦聊天室篇
浅谈skiplist在LevelDB的应用
操作系统常见面试题目:
flannel 原理 之 子网划分
Electronic perpetual calendar of DS1302_ 51 single chip microcomputer, month, day, week, hour, minute and second, lunar calendar and temperature, with alarm clock and complete set of data
LM317的直流可调稳压电源Multisim仿真设计(附仿真+论文+参考资料)
KVM learning resources