题解 | #矩阵交换#
矩阵交换
https://www.nowcoder.com/practice/ec44d4ff8c794b2f9205bdddbde96817
int main()
{
int n = 0, m = 0;
scanf("%d%d", &n, &m);
int arr[10][10] = { 0 };
int i = 0, j = 0;
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
scanf("%d", &arr[i][j]);
}
}
int k = 0;
scanf("%d", &k);
int a = 0, b = 0;
char t=' ';
//循环k次
while(k)
{
getchar();//将缓冲区内空格或回车清除
scanf("%c%d%d",&t,&a, &b);
//行转换
//每次换行或列,需要相对应的减一
if (t == 'r')
{
for (j = 0; j < m; j++)
{
int temp = arr[a-1][j];
arr[a-1][j] = arr[b-1][j];
arr[b-1][j] = temp;
}
}
//列转换
else if (t == 'c')
{
for (i = 0; i < n; i++)
{
int temp = arr[i][a-1];
arr[i][a-1] = arr[i][b-1];
arr[i][b-1] = temp;
}
}
//其它字符直接跳过
else
{
break;
}
k--;
}
//输出
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
printf("%d ", arr[i][j]);
}
printf("\n");
}
return 0;
}