题解 | #矩阵交换#
矩阵交换
http://www.nowcoder.com/practice/ec44d4ff8c794b2f9205bdddbde96817
int main() {
int n, m;
scanf("%d %d", &n, &m);
int val[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%d ", &val[i][j]); //依次输入矩阵元素
}
}
int k, a, b, temp;
scanf("%d ", &k); //输入操作执行次数
char ch[k];
for (int i = 0; i < k; i++) {
scanf("%c %d %d\n", &ch[i], &a, &b); //输入操作方式与操作对象
if (ch[i] == 'r') { //操作为行行变换'r'时
for (int j = 0; j < m; j++) { //操作对象间进行行行交换值
temp = val[a - 1][j];
val[a - 1][j] = val[b - 1][j];
val[b - 1][j] = temp;
}
}
if (ch[i] == 'c') { //操作为行行变换'c'时
for (int k = 0; k < n; k++) { //操作对象间进行列列交换值
temp = val[k][a - 1];
val[k][a - 1] = val[k][b - 1];
val[k][b - 1] = temp;
}
}
}
for (int i = 0; i < n; i++) { //输出变换后矩阵
for (int j = 0; j < m; j++) {
printf("%d ", val[i][j]);
}
printf("\n");
}
return 0;
}