zoukankan      html  css  js  c++  java
  • 1023. 矩阵翻转

    Description

    给定一个正方形的整数矩阵,输出将该矩阵按某一方向翻转后的结果。

    Input Format

    输入第一行有一个整数n,表示一共有n组数据;n不会为负数。

    之后有n组数据,对于每组数据:

    第一行有两个整数a和b,分别表示正方形矩阵的边长,以及翻转的方向。

    当b=0时水平翻转,当b=1时竖直翻转,当b=2时以主对角线为轴翻转。

    b不会取其他值。

    Output Format

    输出共有n组,分别对应n组输入,输出相应矩阵翻转后的结果(仍是一个矩阵)。

    相邻矩阵、相邻行之间没有空行,一行中相邻两个数字之间有且仅有一个空格。

    Sample Input

    2
    2 0
    -2 4
    8 -16
    3 2
    1 2 3
    4 5 6
    7 8 9
    

    Sample Output

    4 -2
    -16 8
    1 4 7
    2 5 8
    3 6 9
    

    Limits

    对于30%的数据,n100

    对于100%的数据,n1000

    对于100%的数据,矩阵的边长a600

     
    
    #include<iostream>
    using namespace std;
    
    int main(){
        int n,a,b;
        //freopen("input.txt","r",stdin);
        cin>>n;
        int t[601][601];
        for(int p=0;p<n;p++){
            cin>>a>>b;
            for(int i=0;i<a;i++){
                for(int j=0;j<a;j++){
                    cin>>t[i][j];
                }
            }
            if(b==0){
                for(int i=0;i<a;i++){
                    for(int j=0;j<a/2;j++){
                        int temp=t[i][j];
                        t[i][j]=t[i][a-1-j];
                        t[i][a-1-j]=temp;
                    }
                }
                for(int i=0;i<a;i++){
                    for(int j=0;j<a;j++){
                        cout<<t[i][j]<<" ";
                    }
                    cout<<endl;
                }
            }
            if(b==1){
                for(int j=0;j<a;j++){
                    for(int i=0;i<a/2;i++){
                        int temp=t[i][j];
                        t[i][j]=t[a-1-i][j];
                        t[a-1-i][j]=temp;
                    }
                }
                for(int i=0;i<a;i++){
                    for(int j=0;j<a;j++){
                        cout<<t[i][j]<<" ";
                    }
                    cout<<endl;
                }            
            }
            if(b==2){
                for(int i=0;i<a;i++){
                    for(int j=0;j<=i;j++){
                        int temp=t[i][j];
                        t[i][j]=t[j][i];
                        t[j][i]=temp;
                    }
                }
                for(int i=0;i<a;i++){
                    for(int j=0;j<a;j++){
                        cout<<t[i][j]<<" ";
                    }
                    cout<<endl;
                }                
            }
        }
        return 0;
    }
  • 相关阅读:
    SEO
    Hack写法
    文学漫步
    [BZOJ4565] [Haoi2016] 字符合并
    [bzoj 3123][Sdoi2013]森林
    [UVA 12633] Super Rooks on Chessboard FFT+计数
    [HDU4609] 3-idiots FFT+计数
    [bzoj4554] [Tjoi2016&Heoi2016]游戏
    [bzoj4556] [Tjoi2016&Heoi2016]字符串
    [bzoj4552][Tjoi2016&Heoi2016]排序
  • 原文地址:https://www.cnblogs.com/bernieloveslife/p/7878606.html
Copyright © 2011-2022 走看看