题解 | #矩阵交换#
矩阵交换
https://www.nowcoder.com/practice/ec44d4ff8c794b2f9205bdddbde96817
#include <stdio.h> int main() { int n, m; scanf("%d %d", &n, &m); int arr1[11][11]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { scanf("%d", &arr1[i][j]); } } int k; scanf("%d", &k); int arr2[5][3]; char arr3[5]; for (int i = 0; i < k; i++) { scanf(" %c %d %d", &arr3[i],&arr2[i][1],&arr2[i][2]); } // -----------------上面位读取数据 ----------------------- for (int i = 0; i < k; i++) { // 操作次数 if (arr3[i] == 'r') { // 行变化 for (int j = 0; j < m; j ++) { // 循环对按行数赋值 int a = arr1[arr2[i][1] - 1][j]; //第arr2[i][1](1)行的第j个值 int b = arr1[arr2[i][2] - 1][j]; //第arr2[i][1](2)行的第j个值 arr1[arr2[i][2] - 1][j] = a; // 交换 arr1[arr2[i][1] - 1][j] = b; } } else if (arr3[i] == 'c') { // 列变化 for (int j = 0; j < n; j ++) { // 循环对按列数赋值 int c = arr1[j][arr2[i][1] - 1]; //第arr2[i][1](1)列的第j个值 int d = arr1[j][arr2[i][2] - 1]; //第arr2[i][1](2)列的第j个值 arr1[j][arr2[i][2] - 1] = c; // 交换 arr1[j][arr2[i][1] - 1] = d; } } } for(int i = 0;i < n;i++){ for(int j = 0;j < m;j++){ printf("%d ",arr1[i][j]); } printf("\n"); } return 0; }