• 【转】【算法题目】顺时针打印矩阵


    这题是《剑指offer》面试题20。不过个人感觉下面这篇文章的讲解更好:

    http://www.cnblogs.com/python27/

      

    题目:给定一个矩阵,从外向内顺时针打印矩阵中的每一个数字。

    例如:给定矩阵:

    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}

    分析:这道题的意思非常直观,给人的感觉也是so easy,然而实际去做的时候会发现,如果结构划分的不好,会出现很多的循环,而且包括对各种边界条件的判定,题目不难,但是给人的感觉——很烦!我分享一下,我做这道题的思路。

    循环条件怎么判定?一个矩阵,给定起点(startX,startY)和终点(endX,endY)(即位于对角线上的两个点)就可以打印一周,然 后向里进一周(即++startX,++startY,--endX,--endY)即可,如果起始点坐标<终止点坐标(即 startX<endX或者startY<endY)循环结束。

    给定起点和终点,打印一周的结束条件是什么?我画了三种情况的矩阵4×4矩阵,3×5矩阵,5×3矩阵(所有矩阵无非就这三种类型,正方形的,偏 “胖”的,偏“瘦”的),很快发现,只有三种情况:一直循环到结束,只剩下一行,只剩下一列。所以我们的函数首先判定:只有一行?打印该行;只有一列,打 印该列。都不是,打印四条边上的数字。

    好了到这里为止:我们可以写出完整的代码了,如下:

    复制代码
      1 #include<iostream>
    2 #include<string>
    3 using namespace std;
    4
    5 void PrintMatrixCircle(int **num,int sX,int sY,int eX,int eY);
    6
    7 //给定矩阵,给定行列,由外向内顺时针打印数字
    8 void PrintMatrixClockwisely(int **matrix,int rows,int columns)
    9 {
    10 if(matrix == NULL || rows < 0 || columns < 0)
    11 return;
    12
    13 int startX = 0;
    14 int startY = 0;
    15 int endX = rows - 1;
    16 int endY = columns - 1;
    17 while(1)
    18 {
    19 if(startX > endX && startY > endY)
    20 break;
    21 if(startX == endX && startY > endY)
    22 break;
    23 if(startX > endX && startY == endY)
    24 break;
    25
    26 PrintMatrixCircle(matrix,startX,startY,endX,endY);
    27
    28 ++startX;
    29 ++startY;
    30 --endX;
    31 --endY;
    32 }
    33 }
    34
    35 //对于给定矩阵,给定对角线上两点,打印这一周的元素
    36 void PrintMatrixCircle(int **num,int sX,int sY,int eX,int eY)
    37 {
    38 //只有一行的情况,直接打印,返回。
    39 if(sX == eX)
    40 {
    41 for(int j = sY;j <= eY;++j)
    42 {
    43 cout<<*(*(num+sX)+j)<<" ";
    44 }
    45
    46 return;
    47 }
    48 //只有一列的情况,直接打印,返回。
    49 if(sY == eY)
    50 {
    51 for(int i = sX;i <= eX;++i)
    52 {
    53 cout<<*(*(num+i)+sY)<<" ";
    54 }
    55 return;
    56 }
    57
    58 //一般的情况打印四行
    59 for(int p = sY;p < eY;++p)
    60 {
    61 cout<<*(*(num+sX)+p)<<" ";
    62 }
    63
    64 for(int q = sX;q < eX;++q)
    65 {
    66 cout<<*(*(num+q)+eY)<<" ";
    67 }
    68
    69 for(int m = eY;m > sY;--m)
    70 {
    71 cout<<*(*(num+eX)+m)<<" ";
    72 }
    73
    74 for(int n = eX;n > sX;--n)
    75 {
    76 cout<<*(*(num+n)+sY)<<" ";
    77 }
    78
    79 }
    80
    81 int main()
    82 {
    83 int example[6][6];
    84 int *point1[6] = {example[0],example[1],example[2],example[3],example[4],example[5]};
    85 int **point = point1;
    86 int cnt=1;
    87
    88 //初始化
    89 for(int i = 0;i < 6;i++) 90 { 91 for(int j = 0;j < 6;j++) 92 { 93 example[i][j] = cnt; 94 cnt++; 95 } 96 } 97 98 cout<<"the original matrix is:"<<endl; 99 for(i = 0;i < 6;i++)100 {101 for(int j = 0;j < 6;j++)102 {103 cout<<example[i][j]<<" ";104 }105 cout<<endl;106 }107 108 cout<<"the clockwisely output of the matrix is:"<<endl;109 110 PrintMatrixClockwisely(point,6,6);111 cout<<endl;112 113 return 0;114 }
    复制代码

    运行结果如下:

    这是正方形矩阵,我也测试了偏”胖“型矩阵和偏”瘦型矩阵“,要测试的话,大家需要对main函数的矩阵和指针做适当修改;

    我的测试用例和测试结果如下:

    偏”胖“型矩阵测试:

    偏”瘦“型矩阵测试:

    题目来源:

    何海涛博客:http://zhedahht.blog.163.com/blog/static/254111742010111112236313/

    注:

    1)本博客所有的代码环境编译均为win7+VC6。所有代码均经过博主上机调试。

    2)博主python27对本博客文章享有版权,网络转载请注明出处http://www.cnblogs.com/python27/。对解题思路有任何建议,欢迎在评论中告知。

  • 相关阅读:
    taotao-manager/taotao-manager-interface/pom.xml
    taotao-manager/taotao-manager-pojo/pom.xml
    taotao-manager/pom.xml
    taotao-manager/taotao-manager-dao/pom.xml
    taotao-common/pom.xml
    taotao-parent/pom.xml(父工程的pom.xml文件配置)
    idea社区版使用maven运行web项目
    IntelliJ IDEA Community社区版集成Tomcat教程
    linux中没有tree命令,command not found,解决办法
    数据导入导出
  • 原文地址:https://www.cnblogs.com/vincently/p/4751414.html
Copyright © 2020-2023  润新知