牛客网|C翻转(北邮往年复试题)

C翻转

题目来源 牛客网-北邮往年复试题
在这里插入图片描述
输入

1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
1 3 1 1

输出

11 6 1 4 5
12 7 2 9 10
13 8 3 14 15
16 17 18 19 20
21 22 23 24 25

#include<iostream>
#include<algorithm>
using namespace std;
void pswap(int *x, int *y) {
    int temp = *y;
    *y = *x;
    *x = temp;
}
int main()
{
	int a[5][5];
	for(int i=0;i<5;i++)
		for(int j=0;j<5;j++)
			cin>>a[i][j];
	int x,y;
	int rotate,cnt;
	cin>>rotate>>cnt>>x>>y;
	int temp[cnt][cnt];
	
	for(int i=0;i<cnt;i++){
		for(int j=0;j<cnt;j++){
			temp[i][j]=a[x+i-1][y+j-1];
		}
	}
	//---------------------------------
	if(rotate==1){
		//翻转 
		for(int i=0;i<cnt;i++){
			for(int j=i;j<cnt;j++){
				pswap(&temp[i][j],&temp[j][i]);
			}
		}
		//对换 
		for(int i=0;i<cnt;i++){
			for(int j=0;j<cnt/2;j++){
				pswap(&temp[i][j],&temp[i][cnt-j-1]);
			}
		}
	}else if(rotate==2){
		//翻转 
		for(int i=0;i<cnt;i++){
			for(int j=i;j<cnt;j++){
				pswap(&temp[i][j],&temp[j][i]);
			}
		}
		//对换 
		for(int j=0;j<cnt;j++){
			for(int i=0;i<cnt/2;i++){
				pswap(&temp[cnt-i-1][j],&temp[i][j]);
			}
		}
	}
	//------------------------------------
	//将temp中的值存入a中 
	for(int i=0;i<cnt;i++){
		for(int j=0;j<cnt;j++){
			a[x+i-1][y+j-1]=temp[i][j];
		}
	}
	for(int i=0;i<5;i++){
		for(int j=0;j<5;j++){
			cout<<a[i][j]<<" ";
		}
		cout<<endl;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值