当前位置:网站首页>矩阵交换行列
矩阵交换行列
2022-04-23 14:13:00 【KissKernel】
##介绍一个关于矩阵交换行列的小问题
###那么我们来看问题吧。
输入描述:
第一行包含两个整数n和m,表示一个矩阵包含n行m列,用空格分隔。 (1≤n≤10,1≤m≤10)
从2到n+1行,每行输入m个整数(范围-231~231-1),用空格分隔,共输入n*m个数,表示第一个矩阵中的元素。
接下来一行输入k,表示要执行k次操作(1≤k≤5)。接下来有k行,每行包括一个字符t和两个数a和b,中间用空格格分隔,t代表需要执行的操作,当t为字符’r’时代表进行行变换,当t为字符’c’时代表进行列变换,a和b为需要互换的行或列(1≤a≤b≤n≤10,1≤a≤b≤m≤10)。
提示:当t为别的字符时不需要处理
输出描述:
输出n行m列,为矩阵交换后的结果。每个数后面有一个空格。
####很显然这个问题的关键就是如何实现行列的交换。那么下面我们来看一下如何交换行列,实际上很简单,就是类似于交换了两个变量。
#include<stdio.h>
int main()
{
int n,m,i,j,k;
int num1,num2;//要交换的两行或两列
char ch;//决定进行行变换还是列变换
int rek;//进行几次操作
int tmp;
scanf("%d %d",&n,&m);
int arr[n][m];
//输入矩阵
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')//进行行变换
{
for(j=0;j<m;j++)
{
tmp=arr[num1-1][j];//进行行变换的时候约定矩阵的行不变循环递归列然后交换即可
arr[num1-1][j]=arr[num2-1][j];
arr[num2-1][j]=tmp;
}
}
else if(ch=='c')//进行列变换
{
for(i=0;i<n;i++)
{
tmp=arr[i][num1-1];//同理进行列变换的时候约定列为需要交换的两列不变后,循环递归循环行即可。
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;
}
当然如果有更简单的方法也欢迎来和我交流这只是方法之一。
版权声明
本文为[KissKernel]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_62745420/article/details/124002194
边栏推荐
- How to do a project easily
- TLS/SSL 协议详解 (28) TLS 1.0、TLS 1.1、TLS 1.2之间的区别
- Redis源码分析之HSET流程与ziplist
- API Gateway/API 网关(二) - Kong的使用 - 负载均衡Loadbalance
- Nacos uses demo as configuration center (IV)
- mysql锁数据库锁
- 返回数组排序后下标
- openstack理论知识
- 常见存储类型和FTP主被动模式解析
- Operation instructions of star boundary text automatic translator
猜你喜欢

A table splitting implementation scheme of MySQL and InnoDB, MyISAM and MRG_ Introduction to MyISAM and other engine application scenarios

krpano全景之vtour文件夹和tour

Use the executors class to quickly create a thread pool

XX project structure notes

剑指offer刷题(1)--面向华为

剑指offer刷题(2)--面向华为

MySQL数据库讲解(八)

Nacos作为配置中心(四) 使用Demo

After entering the new company, the operation and maintenance engineer can understand the deployment of the system from the following items

C语言知识点精细详解——数据类型和变量【2】——整型变量与常量【1】
随机推荐
yml引用其他变量
man man随记和crontab的@reboot用法
uni-app消息推送
SSH 通过跳板机连接远程主机
JS key value judgment
DP - [noip2000] grid access
redis数据库讲解二(redis高可用、持久化、性能管理)
API gateway / API gateway (IV) - use of Kong - Integrated JWT and fuse plug-in
kprobe 的 3 种使用
Use cases of the arrays class
squid代理
MySQL-InnoDB-事务
TLS/SSL 协议详解 (28) TLS 1.0、TLS 1.1、TLS 1.2之间的区别
flannel 原理 之 TUN模式
处理 mkdir:无法创建目录“aaa“:只读文件系统
STD:: map and STD:: vector memory free
编译Openssl
MySQL数据库讲解(九)
Nacos作为配置中心(四) 使用Demo
JS parabola motion packaging method