Ex3_6 m行n列逆转矩阵

时间:2021-05-08 04:03:07
// Ex3_6.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string.h>

void turnLeft(int& directX,int& directY)
{
	int tmp=directX;
	directX=directY;
	directY=-tmp;
}

int main(int argc, char* argv[])
{
	int arry[50][50];
	memset(arry,0,sizeof(arry));

	int m,n;
	printf("Input m,n:");
	scanf("%d,%d",&m,&n);
	getchar();
	int num=1;
	int row=1,col=1;
	int directX=0;
	int directY=1;
	for(int i=1;i<=m;i++)
		for(int j=1;j<=n;j++)
			arry[i][j]=-1;//填充数据区,非填充区为零

while(1)
{
	arry[row][col]=num++;

	if(arry[row+directY][col+directX] !=-1)
	{
		turnLeft(directX,directY);
		if(arry[row+directY][col+directX] !=-1)
			goto output;
	}
	row=row+directY;
	col=col+directX;
}	


output: 
	for( i=1;i<=m;i++)
	{
		for(int j=1;j<=n;j++)
			printf("%3d",arry[i][j]);
		printf("\n");
			
	}


	return 0;
}
/*
Input m,n:4,5
  1 14 13 12 11
  2 15 20 19 10
  3 16 17 18  9
  4  5  6  7  8
Press any key to continue
*/