hdu2135: Rolling table

hdu2135: http://acm.hdu.edu.cn/showproblem.php?pid=2135
题意:旋转一个n*n的矩阵,m为负代表逆时针,m为正代表顺时针,输出旋转后的矩阵 code:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
char v[20][20];
int main()
{
    int m,n;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        getchar();
        for(int i=0;i<n;i++)
            gets(v[i]);
        if((m>0&&m%4==1)||m<0&&(-m)%4==3)
        {
            for(int i=0;i<n;i++)
            {
                for(int j=n-1;j>=0;j--)
                {
                    printf("%c",v[j][i]);
                }
                printf("\n");
            }
        }
        else if((abs(m))%4==2)
        {
            for(int i=n-1;i>=0;i--)
            {
                for(int j=n-1;j>=0;j--)
                {
                    printf("%c",v[i][j]);
                }
                printf("\n");
            }
        }
        else if(m>0&&m%4==3||m<0&&(-m)%4==1)
        {
            for(int i=n-1;i>=0;i--)
            {
                for(int j=0;j<n;j++)
                {
                    printf("%c",v[j][i]);
                }
                printf("\n");
            }
        }
        else
        {
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<n;j++)
                {
                    printf("%c",v[i][j]);
                }
                 printf("\n");
            }

        }
    }
}
/*input:
3 2
123
456
789
3 -1
123
456
789
output:
987
654
321
369
258
147*/

posted on 2012-07-25 21:08  acmer-jun  阅读(148)  评论(0编辑  收藏  举报

导航