Matrix Operation解题报告

来源:互联网 发布:学霸有多努力知乎 编辑:程序博客网 时间:2024/05/29 09:51

题目摘要:You are given a matrix M of type1234x5678. It is initially filled with integers 1...1234x5678 in row major order.Your task is to process a list of commands manipulating M. There are 4 types ofcommands:

"R x y" swap the xth and yth rowof M; 1<=x, y<=1234.

"C x y" swap the xth and ythcolumn of M; 1<=x, y<=5678.

"Q x y" write out M(x, y);1<=x<=1234.1<=y<=5678.

"W z" write out x and y wherez=M(x, y). 1<=z<=7006652 (1234 * 5678)

题目大意:给一个1234行5678列的矩阵用来将从1到1234*5678的数装进去,也就是说,这个矩阵的第一行第一列的数为1,第二列的数为2……第5678列的数为5678。第二行第一列的数为5679,以此类推。然后给出四种命令对应四项操作。R x y 用来交换第x行和第y行的所有数,C x y 用来交换第x列和第y列的所有数。Q x y 用来写出第x行y列的数的值,W z 用来写出数z在此矩阵中位于哪行哪列。

输入输出要求

Input

The input file contains several test cases.The first line is N: the number of test cases. Then follows N lines.

A list of valid commands.1 <= N <=10000.

 

Output

For each "Q x y" write out oneline with the current value of M(x, y), for each "W z" write out oneline with

the value of x and y (described as above)separated by a space.

输入输出样例

Sample Input

10

R 1 2

Q 1 1

Q 2 1

W 1

W 5679

C 1 2

Q 1 1

Q 2 1

W 1

W 5679

 

Sample Output

5679

1

2 1

1 1

5680

2

2 2

1 2

解题思路:先开两个数组X和Y用来存行列变换。初始值为X[1]=1,X[2]=2……X[1234]=1234,Y[1]=1,Y[2]=2……Y[5678]=5678。比如R 1 2,那么第一行和第二行的数互换,此时X[1]=2,X[2]=1,以此类推。W x y=5678*(X[x]-1)+Y[y],Q z用两个for循环搜索一下即可。

代码

#include<iostream>

using namespace std;

int X[1234+5];

int Y[5678+5];

int Q(int x,int y)

{

       return5678*(x-1)+y;

}

int main()

{

       intN;

       intM;

       inti;

       inttemp;

       charch;

       intx,y;

       intm,n;

       for(i=1;i<=1234;i++)

              X[i]=i;

       for(i=1;i<=5678;i++)

              Y[i]=i;

       cin>>N;

       while(N--)

       {

              cin>>ch;

              if(ch=='R')

              {

                     cin>>x>>y;

                     temp=X[x];

                     X[x]=X[y];

                     X[y]=temp;

              }

              if(ch=='C')

              {

                     cin>>x>>y;

                     temp=Y[x];

                     Y[x]=Y[y];

                     Y[y]=temp;

              }

              if(ch=='Q')

              {

                     cin>>x>>y;

                     cout<<Q(X[x],Y[y])<<endl;

              }

              if(ch=='W')

              {

                     cin>>M;

                     m=M%5678;

                  n=(M/5678)+1;

                  if(m==0)

                     {

                            m=5678;

                         n--;

                     }

                  for(x=1;x<=1234;x++)

                     {

                         if(X[x]==n)

                            {

                                cout<<x<<" ";

                                break;

                            }

                     }

                  for(y=1;y<=5678;y++)

                     {

                         if(Y[y]==m)

                            {

                                cout<<y<<endl;

                                break;

                            } 

                     }

              }

       }

       return0;

}

解题感想:这题如果开二维数组的话肯定会爆掉,所以考虑开两个一维数组用来储存行列变换情况,然后算W和Q的对应值就行了。
原创粉丝点击