标签:顺时针打印矩阵
题目:输入一个矩阵,按照从外向里的顺时针的顺序依次打印出每个数字。例如输入矩阵如下:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
则依次打印出数字:1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10
实现如下:
void PrintMatrixClockwisely(int** numbers,int columns,int rows) if(numbers==NULL||columns<=0||rows<=0) return; int start =0; while(columns>start*2&&rows>start*2) { PrintMatrixInCircle(numbers,columns,rows,start); ++start; } } void PrintMatrixInCircle(int** numbers,int columns,int rows,int start) { int endX=columns-1-start; int end=rows-1-start; for(int i-start;i<=endX;++i) { int number=numbers[start][i]; printNumber(number); } if(start<endY) { for(int i=start+1;i<=endY;i++) { int number=numbers[i][endX]; printNumber(number); } } if(start<endX&&start<endY) { for(int i=endX-1;i>=start;i--) { int number=number[endY][i]; printNumber(number); } } if(start<endX&&start<endY-1) { for(int i=endY-1;i>=start+1;i--) { int number=number[i][start]; printNumber(number); } } }
本文出自 “仙路千叠惊尘梦” 博客,请务必保留此出处http://secondscript.blog.51cto.com/9370042/1584933
标签:顺时针打印矩阵
原文地址:http://secondscript.blog.51cto.com/9370042/1584933