当前位置:网站首页>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
边栏推荐
- Upgrade of openssh and modification of version number
- 差分隐私(背景介绍)
- Tongxin UOS uninstall php7 2.24, install php7 4.27 ; Uninstall and then install PHP 7.2.34
- 关于UDP接收icmp端口不可达(port unreachable)
- 解决ssh配置文件优化以及连接慢的问题
- ie8 浏览器提示是否 阻止访问js脚本
- async void 导致程序崩溃
- 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
- LLVM - 生成 if-else 以及 PH
- 基于单片机的DS18B20的数字温度监控报警系统设计【LCD1602显示+Proteus仿真+C程序+论文+按键设置等】
猜你喜欢
Pass in external parameters to the main function in clion
Proteus simulation design of DC adjustable regulated power supply (with simulation + paper and other data)
Use the executors class to quickly create a thread pool
金九银十,入职字节跳动那一天,我哭了(蘑菇街被裁,奋战7个月拿下offer)
关于在vs中使用scanf不安全的问题
C语言知识点精细详解——初识C语言【1】——你不能不知的VS2022调试技巧及代码实操【1】
Mq-2 and DS18B20 fire temperature smoke alarm system design, 51 single chip microcomputer, with simulation, C code, schematic diagram, PCB, etc
Man man notes and @ reboot usage of crontab
差分隐私(背景介绍)
Nacos作为配置中心(四) 使用Demo
随机推荐
直流可调稳压电源的Proteus仿真设计(附仿真+论文等资料)
SHT11传感器的温度湿度监控报警系统单片机Proteus设计(附仿真+论文+程序等)
KVM学习资源
How to do a project easily
剑指offer刷题(1)--面向华为
爬虫练习题(一)
SED 学以致用
C语言知识点精细详解——初识C语言【1】——你不能不知的VS2022调试技巧及代码实操【1】
LLVM - 生成for循环
Electronic scale weighing system design, hx711 pressure sensor, 51 single chip microcomputer (proteus simulation, C program, schematic diagram, thesis and other complete data)
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
source insight via samba
JS key value judgment
Proteus simulation design of DC adjustable regulated power supply (with simulation + paper and other data)
C语言知识点精细详解——初识C语言【1】——你不能不知的VS2022调试技巧及代码实操【2】
Man man notes and @ reboot usage of crontab
C语言知识点精细详解——初识C语言【1】
AT89C51单片机的数字电压表开发,量程0~5V,proteus仿真,原理图PCB和C程序等
Qt界面优化:Qt去边框与窗体圆角化