矩陣旋轉(左旋,右旋)

#include <iostream>
using namespace std;
const int M = 3;	//行數 
const int N = 5;	//列數 

int main() 
{
	int a[M][N] = {{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15}};
	int *p = a[0];
	
	//正常輸出矩陣 
	for(int i=0;i<M;i++)
	{
		for(int j=0;j<N;j++)
		{
			cout<<a[i][j]<<" ";
		}
		cout<<endl;
	}
	cout<<endl;
	
	//右旋
	cout<<"右旋矩陣:"<<endl; 
	for(int i=0;i<N;i++)
	{
		for(int j=M-1;j>=0;j--)
		{ 
			cout<< *(p+j*N+i)<<" ";
		}
		cout<<endl;
	}
	
	//左旋
	cout<<endl;
	cout<<"左旋矩陣:"<<endl; 
	for(int i=N-1;i>=0;i--)
	{
		for(int j=0;j<M;j++)
		{ 
			cout<< *(p+i+j*N)<<" ";
		}
		cout<<endl;
	} 
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章