当前位置:网站首页>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
边栏推荐
- Gif to still image processing
- 51 Single Chip Microcomputer Design of traffic light system (with Proteus simulation, C program, schematic diagram, PCB, thesis and other complete data)
- source insight via samba
- 数组模拟队列进阶版本——环形队列(真正意义上的排队)
- Man man notes and @ reboot usage of crontab
- DS1302的电子万年历_51单片机,年月日、星期、时分秒、农历和温度,带闹钟,全套资料
- 顺序栈的基本操作
- Logical volume creation and expansion
- Introduction to the use of semaphore for inter thread control
- asp.net使用MailMessage发送邮件的方法
猜你喜欢
Man man notes and @ reboot usage of crontab
Mq-2 and DS18B20 fire temperature smoke alarm system design, 51 single chip microcomputer, with simulation, C code, schematic diagram, PCB, etc
TLC5615 based multi-channel adjustable CNC DC regulated power supply, 51 single chip microcomputer, including proteus simulation and C code
TLS/SSL 协议详解 (28) TLS 1.0、TLS 1.1、TLS 1.2之间的区别
Processing MKDIR: unable to create directory 'AAA': read only file system
Matlab Simulink modeling and design of single-phase AC-AC frequency converter, with MATLAB simulation, PPT and papers
本以为能躺着进华为,结果陆续收到京东/滴滴/爱奇艺offer的我迷茫了
剑指offer刷题(1)--面向华为
x509证书cer格式转pem格式
Use the executors class to quickly create a thread pool
随机推荐
Use the executors class to quickly create a thread pool
Golang 对分片 append 是否会共享数据
MySQL同步Could not find first log file name in binary log index file错误
Notes on Visio drawing topology
js 进度条,显示加载进度
微信小程序将原生请求通过es6的promise来进行优化
MySQL基础知识
Preview CSV file
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
51单片机的花卉、农田自动浇水灌溉系统开发,Proteus仿真,原理图和C代码
Tongxin UOS php7 2.3 upgrade to php7.0 two point two four
操作系统常见面试题目:
基于TLC5615的多路可调数控直流稳压电源,51单片机,含Proteus仿真和C代码等
API Gateway/API 网关(四) - Kong的使用 - 集成Jwt和熔断插件
Introduction to loan market quotation interest rate (LPR) and loan benchmark interest rate
Five ways of using synchronized to remove clouds and fog are introduced
Sed learning for application
基于单片机的DS18B20的数字温度监控报警系统设计【LCD1602显示+Proteus仿真+C程序+论文+按键设置等】
Proteus simulation design of DC adjustable regulated power supply (with simulation + paper and other data)
DS1302的电子万年历_51单片机,年月日、星期、时分秒、农历和温度,带闹钟,全套资料