当前位置:网站首页>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
边栏推荐
- Qt实战:云曦聊天室篇
- 错误:无法远程查找到密钥 “428F7ECC7117F726“
- C语言知识点精细详解——初识C语言【1】
- C语言知识点精细详解——初识C语言【1】——你不能不知的VS2022调试技巧及代码实操【1】
- JS recursion (1)
- TUN 设备原理
- Introduction to the use of countdownlatch and cyclicbarrier for inter thread control
- TLS/SSL 协议详解 (28) TLS 1.0、TLS 1.1、TLS 1.2之间的区别
- Logical volume creation and expansion
- man man随记和crontab的@reboot用法
猜你喜欢
Eight way responder system 51 Single Chip Microcomputer Design [with Proteus simulation, C program, schematic diagram, PCB files, component list and papers, etc.]
统信UOS卸载php7.2.24,安装php7.4.27 ;卸载再安装为PHP 7.2.34
统信UOS PHP7.2.3升级至PHP7.2.24
TLS/SSL 协议详解 (28) TLS 1.0、TLS 1.1、TLS 1.2之间的区别
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
1分钟看懂执行流程,永久掌握for循环(附for循环案例)
进入新公司,运维工程师从下面这几项了解系统的部署
Processing MKDIR: unable to create directory 'AAA': read only file system
LotusDB 设计与实现—1 基本概念
随机推荐
KVM学习资源
JS format time
顺序表的操作,你真的学会了吗?
x509证书cer格式转pem格式
API Gateway/API 网关(三) - Kong的使用 - 限流rate limiting(redis)
Usage of BC
tcp_diag 内核相关实现 1 调用层次
Introduction to the use of semaphore for inter thread control
单相交交变频器的Matlab Simulink建模设计,附Matlab仿真、PPT和论文等资料
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
关于UDP接收icmp端口不可达(port unreachable)
JS recursion (1)
Proteus simulation design of four storey and eight storey elevator control system, 51 single chip microcomputer, with simulation and keil c code
百度笔试2022.4.12+编程题目:简单整数问题
Solve the problem of SSH configuration file optimization and slow connection
查找水仙花数-for循环实践
51单片机+LCD12864液晶显示的俄罗斯方块游戏,Proteus仿真、AD原理图、代码、论文等
剑指offer刷题(2)--面向华为
OpenSSH的升级、版本号的修改
Use the executors class to quickly create a thread pool