面试题20 顺时针打印矩阵

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



[cpp]  view plain copy
  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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Python面试中,有一些常见的问题被问及。以下是一些常见的Python面试题的例子: 1. Python中的列表和元组有什么区别? 2. 什么是Python中的装饰器?它们有什么作用? 3. 解释一下Python中的异常处理机制。 4. 什么是迭代器和生成器?它们的区别是什么? 5. Python中的多线程和多进程有什么区别? 6. 什么是Python中的垃圾回收机制? 7. 解释一下Python中的装饰器?它们有什么作用? 8. 如何在Python中处理文件? 9. 什么是Python中的模块?如何使用模块? 10. 解释一下Python中的虚拟环境。 11. 如何在Python中进行单元测试? 12. 解释一下Python中的深拷贝和浅拷贝。 13. 什么是Python中的闭包? 14. 如何处理Python中的日期和时间? 15. 解释一下Python中的面向对象编程。 16. 什么是Python中的装饰器?它们有什么作用? 17. 如何在Python中处理异常? 18. 解释一下Python中的迭代器和生成器。 19. 什么是Python中的装饰器?它们有什么作用? 20. 解释一下Python中的数据结构和算法。 这些问题涵盖了Python基础知识、编程概念、常见问题和最佳实践等多个方面。提前准备这些问题的答案可以帮助你在面试中脱颖而出。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [吐血总结!20道Python面试题集锦(附答案)](https://blog.csdn.net/weixin_45342712/article/details/95757611)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值