面试题20 顺时针打印矩阵

来源:互联网 发布:icmp端口53 编辑:程序博客网 时间:2024/05/16 12:31

分析:把矩阵想象成若干个圈,用一个循环打印矩阵,每次打印矩阵的一个圈



[cpp] view plaincopy
  1. #include "stdafx.h"  
  2. #include <iostream>  
  3. using namespace std;  
  4.   
  5. void PrintMatrixIncircle(int **nArr, int rows, int columns, int nStart)  
  6. {  
  7.     int nEndX = columns - 1 -nStart;  
  8.     int nEndY = rows - 1 -nStart;  
  9.     //从左到右打印一行  
  10.     for (int i=nStart; i<=nEndX; i++)  
  11.     {  
  12.         cout << nArr[nStart][i] << " ";  
  13.     }  
  14.   
  15.     //从上到下打印一列  
  16.     if (nEndY > nStart)  
  17.     {         
  18.         for (int j=nStart+1; j<=nEndY; j++)  
  19.         {  
  20.             cout << nArr[j][nEndX] << " ";  
  21.         }  
  22.     }  
  23.       
  24.     //从右到左打印一行  
  25.     if (nEndY > nStart && nEndX > nStart)  
  26.     {         
  27.         for (int t=nEndX-1; t>=nStart; t--)  
  28.         {  
  29.             cout << nArr[nEndY][t] << " ";  
  30.         }  
  31.     }  
  32.   
  33.     //从下到上打印一列  
  34.     if (nEndY -1 > nStart && nEndX > nStart)  
  35.     {         
  36.         for (int n=nEndY-1; n>=nStart+1; n--)  
  37.         {  
  38.             cout << nArr[n][nStart] << " ";  
  39.         }  
  40.     }     
  41. }  
  42.   
  43. //顺时针打印矩阵,行数为rows,列数为columns  
  44. void PrintMatrixClockWisely(int **nArr, int rows, int columns)  
  45. {  
  46.    if (nArr == NULL || rows <= 0 || columns <= 0)  
  47.    {  
  48.        return;  
  49.    }  
  50.   
  51.    int nStart = 0;  
  52.    while (rows>(nStart*2) && columns>(nStart*2))     
  53.    {  
  54.        PrintMatrixIncircle(nArr, rows, columns, nStart);  
  55.        nStart++;  
  56.    }  
  57. }  
  58.   
  59.   
  60. int _tmain(int argc, _TCHAR* argv[])  
  61. {  
  62.     int nMatrix1[4][4] = {{1,2,3,4},{5, 6, 7, 8},{9, 10, 11, 12},{13, 14, 15, 16}};  
  63.     int **pp1 = new int*[4];  
  64.     for (int i=0; i<4; i++)  
  65.     {  
  66.         pp1[i] = nMatrix1[i];  
  67.     }  
  68.     PrintMatrixClockWisely(pp1, 4, 4);  
  69.     cout << endl;  
  70.   
  71.     int nMatrix2[1][4] = {{1,2,3,4}};  
  72.     int **pp2 = new int*[1];  
  73.     for (int i=0; i<1; i++)  
  74.     {  
  75.         pp2[i] = nMatrix2[i];  
  76.     }  
  77.     PrintMatrixClockWisely(pp2, 1, 4);  
  78.     cout << endl;  
  79.   
  80.     int nMatrix3[4][1] = {{1},{2},{3},{4}};  
  81.     int **pp3 = new int*[4];  
  82.     for (int i=0; i<4; i++)  
  83.     {  
  84.         pp3[i] = nMatrix3[i];  
  85.     }  
  86.     PrintMatrixClockWisely(pp3, 4, 1);  
  87.     cout << endl;  
  88.   
  89.     int nMatrix4[2][5] = {1,2,3,4,5,6,7,8,9,10};  
  90.     int **pp4 = new int*[2];  
  91.     for (int i=0; i<2; i++)  
  92.     {  
  93.         pp4[i] = nMatrix4[i];  
  94.     }  
  95.     PrintMatrixClockWisely(pp4, 2, 5);  
  96.     cout << endl;  
  97.   
  98.     system("pause");  
  99.     return 0;  
  100. }  

运行结果:





0 0
原创粉丝点击