题解 | #矩阵交换#

矩阵交换

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

#include <stdio.h>
int main()
{
    int n=0;
    int m=0;
    scanf("%d %d",&n,&m);
    int arr[10][10]={0};
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            scanf("%d",&arr[i][j]);
        }
    }
    int k=0;//变换的次数
    scanf("%d",&k);
    while(k--)
    {
        char flag='c';
        int b=0;
        int c=0;
        scanf(" %c%d%d",&flag,&b,&c);//这里的空格非常重要,自动过滤掉首位置的一切空白字符
        b--;
        c--;
        if(flag=='r')
        {
           
            for(int i=0;i<m;i++)
            {
                int temp=arr[b][i];
                arr[b][i]=arr[c][i];
                arr[c][i]=temp;
            }
        }
       else if(flag =='c')
        {
            
            for(int i=0;i<n;i++)
            {
            int temp=arr[i][b];
            arr[i][b]=arr[i][c];
            arr[i][c]=temp;
            }
        }
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            printf("%d ",arr[i][j]);
        }
        printf("\n");
    }
    return 0;
}

全部评论

相关推荐

uu们,拒offer时hr很生气怎么办我哭死
爱睡觉的冰箱哥:人家回收你的offer,或者oc后没给你发offer的时候可不会愧疚你,所以你拒了也没必要愧疚他。
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-03 18:22
投了几百份简历,专业和方向完全对口,都已读不回。尝试改了一下学校,果然有奇效。
steelhead:这不是很正常嘛,BOSS好的是即便是你学院本可能都会和聊几句,牛客上学院本机会很少了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务