题解 | #矩阵交换#

矩阵交换

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

#include<stdio.h>
int main() 
{
    int arr[10][10] = { 0 }, n, m;
    scanf("%d %d", &n, &m);
    int i,j;
    for ( i = 0; i < n; i++)
        for ( j = 0; j < m; j++)
            scanf("%d", &arr[i][j]);

    int k,a,b;
    char t = 0;
    scanf("%d", &k);
    for(i=0;i<k;i++)
    {
        getchar();
        scanf("%c %d %d", &t, &a, &b);
        int tmp = 0;
        a -= 1, b -= 1;
        if (t == 'r') 
        {
            //行变换
            for (int j = 0; j < m; j++) 
            {
                tmp = arr[a][j];
                arr[a][j] = arr[b][j];
                arr[b][j] = tmp;
            }
        } else if (t == 'c') 
        {
            //列变换
            for (int i = 0; i < n; i++) 
            {
                tmp = arr[i][a];
                arr[i][a] = arr[i][b];
                arr[i][b] = tmp;
            }
        }
    }

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

全部评论

相关推荐

我已成为0offer的糕手:别惯着,胆子都是练出来的,这里认怂了,那以后被裁应届被拖工资还敢抗争?
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务