顺时针打印矩阵

简介:

   输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字。


    解题思路:我们把打印一圈分为四步:第一步从左到右打印一行,第二步从上到下打印一列,第三步从右到左打印一行,第四步从下到上打印一列。值得注意的是,最后一圈有可能退化成只有一行、只有一列,甚至只有一个数字。

    因此要仔细分析打印时每一步的前提条件。第一步总是需要的,因为打印一圈至少有一步。如果只有一行,就不用第二步了。也就是需要第二步的前提条件是终止行号大于起始行号。需要第三部打印的前提条件是圈内至少有两行两列,也就是说除了要求终止行号大于起始行号之外,还要求终止列号大于起始列号。需要打印第四步的前提条件是至少有三行两列,因此要求终止行号比起始行号至少大2,终止列号大于起始列号。


C#实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#region 顺时针打印矩阵
         /// 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字。
         /// 
         public  static  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++;
             }
         }
 
         private  static  void  PrintMatrixInCircle( int [,] numbers,  int  columns,  int  rows,  int  start)
         {
             int  endX = columns - 1 - start;
             int  endY = rows - 1 - start;
 
             // 从左到右打印一行
             for  ( int  i = start; i <= endX; i++)
                 Console.Write(numbers[start, i] +  "," );
 
             // 从上到下打印一列
             if  (start < endY)
                 for  ( int  i = start + 1; i <= endY; i++)
                     Console.Write(numbers[i, endX] +  "," );
             
             // 从右到左打印一行
             if  (start < endX && start < endY)
                 for  ( int  i = endX - 1; i >= start; i--)
                     Console.Write(numbers[endY, i] +  "," );
             // 从下到上打印一行
             if  (start < endX && start < endY - 1)
                 for  ( int  i = endY - 1; i >= start + 1; i--)
                     Console.Write(numbers[i, start] +  "," );
 
         }
         #endregion

Java实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/** 顺时针打印矩阵
      * 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字。
      */
     public  static  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++;
         }
     }
     
     private  static  void  printMatrixInCircle( int [][] numbers,  int  columns,  int  rows,  int  start)
     {
         int  endX = columns -  1  - start;
         int  endY = rows -  1  - start;
 
         // 从左到右打印一行
         for  ( int  i = start; i <= endX; i++)
             System.out.print(numbers[start][i] +  "," );
 
         // 从上到下打印一列
         if  (start < endY)
             for  ( int  i = start +  1 ; i <= endY; i++)
                 System.out.print(numbers[i][endX] +  "," );
         
         // 从右到左打印一行
         if  (start < endX && start < endY)
             for  ( int  i = endX -  1 ; i >= start; i--)
                 System.out.print(numbers[endY][i] +  "," );
         // 从下到上打印一行
         if  (start < endX && start < endY -  1 )
             for  ( int  i = endY -  1 ; i >= start +  1 ; i--)
                 System.out.print(numbers[i][start] +  "," );
 
     }

Python实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
def  printMatrixClockWisely(numbers, columns, rows):
     """
     顺时针打印矩阵
     输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字。
     :param numbers:
     :param columns:
     :param rows:
     :return:
     """
     if  numbers  = =  None  or  columns < =  0  or  rows < = 0 :
         return
     start  =  0
     while  columns > start  *  2  and  rows > start  * 2 :
         printMatrixInCircle(numbers, columns, rows, start)
         start  + = 1
 
def  printMatrixInCircle(numbers, columns, rows, start):
     endX  =  columns  -  1  -  start
     endY  =  rows  -  1  -  start
 
     # 从左到右打印一行
     for  in  range (start, endX + 1 ):
         print (numbers[start][i], end = ', ' )
     # 从上到下打印一列
     if  start < endY:
         for  in  range (start + 1 , endY + 1 ):
             print (numbers[i][endX], end = ', ' )
     # 从右到左打印一行
     if  start < endX  and  start < endY:
         for  in  range (endX  -  1 , start - 1 - 1 ):
             print (numbers[endY][i], end = ', ' )
     # 从下到上打印一行
     if  start < endX  and  start < endY  -  1 :
         for  in  range (endY - 1 , start,  - 1 ):
             print (numbers[i][start], end = ', ' )



本文转自 许大树 51CTO博客,原文链接:http://blog.51cto.com/abelxu/1973293,如需转载请自行联系原作者
相关文章
|
8天前
|
人工智能 运维 安全
|
6天前
|
人工智能 异构计算
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
|
7天前
|
机器学习/深度学习 人工智能 自然语言处理
B站开源IndexTTS2,用极致表现力颠覆听觉体验
在语音合成技术不断演进的背景下,早期版本的IndexTTS虽然在多场景应用中展现出良好的表现,但在情感表达的细腻度与时长控制的精准性方面仍存在提升空间。为了解决这些问题,并进一步推动零样本语音合成在实际场景中的落地能力,B站语音团队对模型架构与训练策略进行了深度优化,推出了全新一代语音合成模型——IndexTTS2 。
642 22
|
7天前
|
人工智能 测试技术 API
智能体(AI Agent)搭建全攻略:从概念到实践的终极指南
在人工智能浪潮中,智能体(AI Agent)正成为变革性技术。它们具备自主决策、环境感知、任务执行等能力,广泛应用于日常任务与商业流程。本文详解智能体概念、架构及七步搭建指南,助你打造专属智能体,迎接智能自动化新时代。
|
13天前
|
人工智能 JavaScript 测试技术
Qwen3-Coder入门教程|10分钟搞定安装配置
Qwen3-Coder 挑战赛简介:无论你是编程小白还是办公达人,都能通过本教程快速上手 Qwen-Code CLI,利用 AI 轻松实现代码编写、文档处理等任务。内容涵盖 API 配置、CLI 安装及多种实用案例,助你提升效率,体验智能编码的乐趣。
1045 110
人工智能 数据可视化 数据挖掘
235 0