题解 | 矩阵交换

%c前面加个空格就可以输出了,看了解析里面的一位大佬才解决这个问题

#include <stdio.h>

int main() {
    int n = 0;
    int m = 0;
    int arr[10][10] = { 0 };
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            scanf("%d", &arr[i][j]);
        }
    }
    int k;
    scanf("%d", &k);
    char instruction;
    int a, b;
    for (int t = 0; t < k; t++) {
        scanf(" %c %d %d", &instruction, &a, &b);
        a -= 1;
        b -= 1;
        if (instruction == 'r') {
            for (int c = 0; c < m; c++) {
                int tem = arr[a][c];
                arr[a][c] = arr[b][c];
                arr[b][c] = tem;
            }
        }
        else if (instruction == 'c') {
            for (int r = 0; r < n; r++) {
                int tem = arr[r][a];
                arr[r][a] = arr[r][b];
                arr[r][b] = tem;
            }
        }
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            printf("%d ", arr[i][j]);
        }
        printf("\n");
    }

    return 0;
}

全部评论

相关推荐

03-11 21:46
西北大学 Java
河和静子:这只是实习工资,我学长北大通班博一的,他同学被这家天天发邮件让他去实习,一个月10w
点赞 评论 收藏
分享
码农索隆:我头回见校招简历把个人优势写在最前面的,是我老了吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务