当前位置:网站首页>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
边栏推荐
- js 抛物线运动方法封装
- 网页自适应,等比缩放
- 百度笔试2022.4.12+编程题目:简单整数问题
- TLC5615 based multi-channel adjustable CNC DC regulated power supply, 51 single chip microcomputer, including proteus simulation and C code
- JS progress bar, displaying the loading progress
- XX project structure notes
- SHT11传感器的温度湿度监控报警系统单片机Proteus设计(附仿真+论文+程序等)
- 逻辑卷创建与扩容
- 编译Openssl
- 电子秤称重系统设计,HX711压力传感器,51单片机(Proteus仿真、C程序、原理图、论文等全套资料)
猜你喜欢

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

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

TLS/SSL 协议详解 (30) SSL中的RSA、DHE、ECDHE、ECDH流程与区别

x509证书cer格式转pem格式

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

AT89C52单片机的频率计(1HZ~20MHZ)设计,LCD1602显示,含仿真、原理图、PCB与代码等

Proteus simulation design of four storey and eight storey elevator control system, 51 single chip microcomputer, with simulation and keil c code

C语言知识点精细详解——初识C语言【1】

Mq-2 and DS18B20 fire temperature smoke alarm system design, 51 single chip microcomputer, with simulation, C code, schematic diagram, PCB, etc

Nacos作为配置中心(四) 使用Demo
随机推荐
Processing MKDIR: unable to create directory 'AAA': read only file system
C语言知识点精细详解——初识C语言【1】
C语言知识点精细详解——数据类型和变量【2】——整型变量与常量【1】
c语言在结构体传参时参数压栈问题
gif转为静态图片处理
Some little records~
数组模拟队列进阶版本——环形队列(真正意义上的排队)
Qt实战:云曦日历篇
Use cases of the arrays class
51单片机的花卉、农田自动浇水灌溉系统开发,Proteus仿真,原理图和C代码
常见存储类型和FTP主被动模式解析
Pass in external parameters to the main function in clion
处理 mkdir:无法创建目录“aaa“:只读文件系统
API Gateway/API 网关(三) - Kong的使用 - 限流rate limiting(redis)
IE8 browser prompts whether to block access to JS script
1分钟看懂执行流程,永久掌握for循环(附for循环案例)
Solve the problem of SSH configuration file optimization and slow connection
OpenSSH的升级、版本号的修改
初始c语言大致框架适合复习和初步认识
四层和八层电梯控制系统Proteus仿真设计,51单片机,附仿真和Keil C代码