标签:




#ifndef TIME_WISE_PRINT_MATRIX_H#define TIME_WISE_PRINT_MATRIX_H#include<iostream>#define COLUS 3void printMatrixCore(int (*)[COLUS],int rows,int);void timewisePrintMatrix(int (*matrix)[COLUS],int rows){if(matrix==NULL){return;}int printCount=0;while(2*printCount<COLUS&&2*printCount<rows){printMatrixCore(matrix,rows,printCount);printCount++;}}void printMatrixCore(int (*matrix)[COLUS],int rows,int count){int rowStart=count;int rowEnd=rows-count-1;int colStart=count;int colEnd=COLUS-count-1;//print the first rowsfor(int i=colStart; i<=colEnd; i++){std::cout<<matrix[rowStart][i]<<",";}//print the second columnif(rowEnd-rowStart>1){for(int i=rowStart+1; i<=rowEnd-1; i++){std::cout<<matrix[i][colEnd]<<",";}}//print the second rowif(rowEnd-rowStart>=1){for(int i=colEnd; i>=colStart; i--){std::cout<<matrix[rowEnd][i]<<",";}}//print the first colif(rowEnd-rowStart>1&&colEnd-colStart>=1){for(int i=rowEnd-1; i>=rowStart+1; i--){std::cout<<matrix[i][colStart]<<",";}}}#endif
#include"timeWisePrintMatrix.h"#include<iostream>int main(){int matrix[3][3]={{1,2,3},{4,5,6},{7,8,9}};timewisePrintMatrix(matrix,3);}

标签:
原文地址:http://www.cnblogs.com/yml435/p/4673983.html