Friday, August 11, 2017

UVa problem solution 11360 - Have Fun with Matrices

problem link:


Discuss: in this problem you just do work with 2D array. to see the input not use string to take input instead you can use "%1d" specifier.then when do transpose matrix you know what is transpose matrix just exchange the variable [i][j] and [j][i].when you do column exchange you can do it with one loop.just exchange the [i][column1] and [i][coulmn2].when do row exchange you can do same thing just [row1][i] and [row2][i] variable exchange.  when you increment and dic you can use modular '%'. it will work. after all it is just a easy problem..


try your self before see the code



#include<bits/stdc++.h>
using namespace std;
int main()
{

    int t,k;
    cin >>t;
   for(k=1; k <= t; k++)
    {
        int n,i,j,m,x,y;
     int ara[15][15];
        cin >>n;
        for(i = 0; i< n; i++)
        {

            for(j = 0; j < n; j++)
            {
                scanf("%1d",&ara[i][j]);
            }
        }
        cin >>m;
        for(int l=0;l<m;l++)
        {
            char str[150];
           int  temp,tt,trans[15][15];
            scanf("%s",&str);
            if(!strcmp(str,"row"))
            {
                cin >>x>>y;
                x--;y--;
                    for(i=0;i<n;i++)
                    {
                        temp=ara[x][i];
                        ara[x][i]=ara[y][i];
                        ara[y][i]=temp;
                }
            }
           else if(!strcmp(str,"col"))
            {
                cin >>x>>y;
               x--;y--;
                for(i =0;i<n;i++)
                {

                    temp=ara[i][x];
                    ara[i][x]=ara[i][y];
                    ara[i][y]=temp;

                }

            }
           else if(!strcmp(str,"inc"))
           {
               for(i=0;i<n;i++)
               {
                   for(j=0;j<n;j++)
                   {

                       ara[i][j]=ara[i][j]+1;
                       if(ara[i][j]==10)
                        ara[i][j]=0;
                   }
               }
           }
            else if(!strcmp(str,"dec"))
           {
               for(i=0;i<n;i++)
               {
                   for(j=0;j<n;j++)
                   {
                       ara[i][j]=(ara[i][j]+9)% 10;
                       if(ara[i][j]<0)
                       ara[i][j]=9;
                   }
               }
           }
           else if(!strcmp(str,"transpose"))
           {

               for(i=0;i<n;i++)
               {
                   for(j=i+1;j<n;j++)
                   {
                       temp=ara[i][j];
                       ara[i][j]=ara[j][i];
                       ara[j][i]=temp;
                   }
               }
           }
        }
        printf("Case #%d\n",k);
       for(i=0;i<n;i++)
       {
           for(j=0;j<n;j++)
           {
               printf("%d",ara[i][j]);
           }
                 printf("\n");
       }
        printf("\n");
    }
    return 0;
}

No comments:

Post a Comment