C++将正方形矩阵的第一行和最后一行互换,就是在输出时判断如果是第一行那就输出最后一行,如果是最后一行那就输出第一行。

输入

3
7 8 9
4 5 6
1 2 3

输出

1 2 3
4 5 6
7 8 9
#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin>>n;
    int arr[n+5][n+5];
    for(int i=0;i<n;i++){
    	for(int j=0;j<n;j++){
    		cin>>arr[i][j];
		}
	}
	cout<<endl;
	for(int i=0;i<n;i++){
		if(i==0){
			for(int j=0;j<n;j++){
				cout<<arr[n-1][j]<<" ";
			}
			cout<<endl;
			continue;
		}else if(i==n-1){
			for(int j=0;j<n;j++){
				cout<<arr[0][j]<<" ";
			}
			cout<<endl;
			continue;
		}else{
			for(int j=0;j<n;j++){
			    cout<<arr[i][j]<<" ";	
			}
			cout<<endl;
		}
	}
	return 0;
}

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐