题解 | #矩阵交换#
矩阵交换
https://www.nowcoder.com/practice/ec44d4ff8c794b2f9205bdddbde96817
#include <stdio.h> #define N 10 #define M 10 int main() { int n = 0; int m = 0; while (2 == scanf("%d %d", &n, &m)) { int arr[N][M] = {0}; int k = 0; char t = 0; int a = 0; int b = 0; // 输入 for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { scanf("%d", *(arr + i) + j); } } scanf("%d", &k); while (k--) { getchar();//吸收换行符 scanf("%c %d %d", &t, &a, &b); if ('r' == t) { // 行交换 for (int j = 0; j < m; j++) { int tmp = arr[a - 1][j]; arr[a - 1][j] = arr[b - 1][j]; arr[b - 1][j] = tmp; } } else if ('c' == t) { // 列交换 for (int i = 0; i < n; i++) { int tmp = arr[i][a - 1]; arr[i][a - 1] = arr[i][b - 1]; arr[i][b - 1] = tmp; } } else { ; } } // 输出 for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { printf("%d ", arr[i][j]); } printf("\n"); } } return 0; }