码迷,mamicode.com
首页 > 其他好文 > 详细

1023. 矩阵翻转

时间:2017-11-22 14:26:28      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:pad   namespace   sample   math   block   str   空格   sam   script   

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;
}

 

1023. 矩阵翻转

标签:pad   namespace   sample   math   block   str   空格   sam   script   

原文地址:http://www.cnblogs.com/bernieloveslife/p/7878606.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!