当前位置:网站首页>矩阵交换行列
矩阵交换行列
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
边栏推荐
- Mysql的安装过程(已经安装成功的步骤说明)
- JS progress bar, displaying the loading progress
- Operation instructions of star boundary text automatic translator
- MySQL同步Could not find first log file name in binary log index file错误
- 贷款市场报价利率(LPR)与贷款基准利率介绍
- Qt实战:云曦聊天室篇
- Web page, adaptive, proportional scaling
- Flop effect
- js 递归(1)
- 如何5分钟上手使用OCR
猜你喜欢
C语言知识点精细详解——初识C语言【1】
API Gateway/API 网关(三) - Kong的使用 - 限流rate limiting(redis)
Some experience of using dialogfragment and anti stepping pit experience (getactivity and getdialog are empty, cancelable is invalid, etc.)
ThreadGroup ThreadGroup implémente l'interface threadfactory en utilisant la classe Introduction + Custom thread Factory
Storage path of mod subscribed by starbound Creative Workshop at Star boundary
asp.net使用MailMessage发送邮件的方法
MySQL数据库讲解(九)
krpano全景之vtour文件夹和tour
差分隐私(背景介绍)
MySQL-InnoDB-事务
随机推荐
JDBC details
API Gateway/API 网关(三) - Kong的使用 - 限流rate limiting(redis)
Use the executors class to quickly create a thread pool
How does void * exist?
bc的用法
Operation instructions of star boundary automatic text translator (advanced version)
Introduction to the use of countdownlatch and cyclicbarrier for inter thread control
XX project structure notes
Use cases of the arrays class
MySQL-InnoDB-事务
Date的after时间判断
man man随记和crontab的@reboot用法
Redis源码分析之HSET流程与ziplist
关于UDP接收icmp端口不可达(port unreachable)
ansible及常用模块的使用
MySQL数据库讲解(八)
krpano全景之vtour文件夹和tour
线程组ThreadGroup使用介绍+自定义线程工厂类实现ThreadFactory接口
Installation and use of postman pit
MySQL lock database lock