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

 

转载于:https://www.cnblogs.com/bernieloveslife/p/7878606.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值