题解 | #矩阵交换#

矩阵交换

https://www.nowcoder.com/practice/ec44d4ff8c794b2f9205bdddbde96817

#include <stdio.h>
int main() {
    int n, m;
    scanf("%d %d", &n, &m);
    int a[10][10];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            scanf("%d", &a[i][j]);
        }
    }
    int k;
    scanf("%d", &k);
    char t;
    while(k!=0)
    {
        while (scanf("%c", &t) != EOF) {
            int x, y;
            scanf("%d %d", &x, &y);
            if (t == 'r') {
                for (int j = 0; j < m; j++) {
                    int t=a[x - 1][j];
                    a[x - 1][j] = a[y - 1][j];
                    a[y - 1][j]=t;
                }
            } else 
                if (t == 'c') {
                for (int i = 0; i < n; i++) {
                    int t=a[i][x - 1];
                    a[i][x - 1] = a[i][y - 1];
                    a[i][y - 1]=t;
                }
            } 
        }
        k--;
    }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                printf("%d ", a[i][j]);
            }
            printf("\n");
        }
    return 0;
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务