当前位置:网站首页>Matrix exchange row and column

Matrix exchange row and column

2022-04-23 14:25:00 KissKernel

## Introduce a small problem about matrix exchange rows and columns

### So let's look at the problem .
Input description :
The first line contains two integers n and m, Indicates that a matrix contains n That's ok m Column , Separate... With spaces . (1≤n≤10,1≤m≤10)

from 2 To n+1 That's ok , Enter... On each line m It's an integer ( Range -231~231-1), Separate... With spaces , Co input n*m Number , Represents the elements in the first matrix .
On the next line, type k, To perform k operations (1≤k≤5). Next there is k That's ok , Each line contains one character t And two numbers a and b, The middle is separated by an empty grid ,t Represents the action to be performed , When t Character ’r’ Time represents row transformation , When t Character ’c’ Time represents column transformation ,a and b For rows or columns that need to be interchanged (1≤a≤b≤n≤10,1≤a≤b≤m≤10).

Tips : When t For other characters, there is no need to deal with
Output description :
Output n That's ok m Column , Is the result of matrix exchange . There is a space after each number .
#### Obviously, the key to this problem is how to realize the exchange of ranks . Now let's take a look at how to exchange ranks , It's actually very simple , It's like swapping two variables .

#include<stdio.h>

int main()
{
    
    int n,m,i,j,k;
    int num1,num2;// Two rows or two columns to swap 
    char ch;// Decide whether to transform rows or columns 
    int rek;// Perform several operations 
    int tmp;
    scanf("%d %d",&n,&m);
    int arr[n][m];
    // Input matrix 
    for(i=0;i<n;i++)
    {
    
        for(j=0;j<m;j++)
        {
    
            scanf("%d",&arr[i][j]);
        }
    }
    
    scanf("%d",&k);
    
    for(rek=0;rek<k;rek++)
    {
    
       scanf(" %c %d %d",&ch,&num1,&num2); 
        if(ch=='r')// Perform line transformation 
        {
    
            for(j=0;j<m;j++)
            {
    
                tmp=arr[num1-1][j];// When performing row transformation, agree that the rows of the matrix are invariant, cycle recursive columns, and then exchange 
                arr[num1-1][j]=arr[num2-1][j];
                arr[num2-1][j]=tmp;
            }
        }
        else if(ch=='c')// Perform column transformation 
        {
    
            for(i=0;i<n;i++)
            {
    
                tmp=arr[i][num1-1];// Similarly, when performing column transformation, it is agreed that the two columns to be exchanged remain unchanged , Loop recursion, loop lines .
                arr[i][num1-1]=arr[i][num2-1];
                arr[i][num2-1]=tmp;
            }
        }

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

    
    return 0;
}

Of course, if there is a simpler method, you are welcome to communicate with me. This is only one of the methods .

版权声明
本文为[KissKernel]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231412251933.html