矩阵交换
#include<stdio.h>
int main()
{
int n = 0;//行
int m = 0;//列
int arr[10][10] = { 0 };//原数组
int arr1[10][10] = { 0 };//变换存储在中间数组
int k = 0;//操作次数
char t[5] = { '0' };//操作指令数组r c
int a[5] = { 0 };
int b[5] = { 0 };
scanf("%d%d", &n, &m);//获取行和列
for (int i = 0; i < n; i++)//获取原矩阵
for (int j = 0; j < m; j++)
{
scanf("%d", &arr[i][j]);
arr1[i][j] = arr[i][j];//并将原矩阵数据给中间矩阵
}
scanf("%d", &k);//获取操作次数
for (int i = 0; i < k; i++)
{
scanf(" %c",&t[i]);//获取操作指令
scanf("%d %d", &a[i], &b[i]);
}
for (int w = 0; w < k; w++)//进行k次操作
{
if (t[w] == 'r')//操作指令为r,就进行换行操作
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (i == a[w] - 1)//原来a行数据交换成b行的数据
{
arr1[i][j] = arr[b[w] - 1][j];
}
else if (i == b[w] - 1)//原来b行数据交换成a行的数据
{
arr1[i][j] = arr[a[w] - 1][j];
}
else
{
arr1[i][j] = arr[i][j];//其他没动的地方不操作
}
}
}
for (int i = 0; i < n; i++)//操作完后将中间矩阵arr1中数据更新给arr
{
for (int j = 0; j < m; j++)
{
arr[i][j] = arr1[i][j];
}
}
}
else if (t[w] == 'c')//操作指令为c,就进行换列操作
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (j == a[w] - 1)
{
arr1[i][j] = arr[i][b[w] - 1];
}
else if (j == b[w] - 1)
{
arr1[i][j] = arr[i][a[w] - 1];
}
else
{
arr[i][j] = arr1[i][j];
}
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
arr[i][j] = arr1[i][j];
}
}
}
}
for (int i = 0; i < n; i++)//打印中间矩阵arr1或者原矩阵arr的数据都可
{
for (int j = 0; j < m; j++)
{
printf("%d ", arr1[i][j]);
}
printf("\n");//打印一行就换行
}
return 0;
}