【面试题020】顺时针打印矩阵

【面试题020】顺时针打印矩阵 

 

输入一个矩阵,按照从外到里的顺序打印出每一个数字,

如果只有一行,那么就不用第二步了,

第二步的前提条件是终止行号大于起始行号。

第三步的前提条件是圈内至少要有两行两列,也就是说除了终止行号要大于起始行号外,还要求终止利好大于起始列号;

同理第四步的前提条件是要有三行两列,因此要求终止行号比起始行号至少大2,同时终止列号大于起始列号;

 

PrintMatrix.cpp:

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
 
#include <iostream>
#include <cstdio>

using  namespace std;

void PrintMatrixInCircle( int **numbers,  int columns,  int rows,  int start);
void PrintNumber( int number);

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;
    }
}

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)
    {
         int number = numbers[start][i];
        PrintNumber(number);
    }
     /*从上到下打印一列*/
     if(start < endY)
    {
         for( int i = start +  1; i <= endY; ++i)
        {
             int number = numbers[i][endX];
            PrintNumber(number);
        }
    }
     /*从右到左打印一行*/
     if(start < endX && start < endY)
    {
         for( int i = endX -  1; i >= start; --i)
        {
             int number = numbers[endY][i];
            PrintNumber(number);
        }
    }
     /*从下到上打印一行*/
     if(start < endX && start < endY -  1)
    {
         for( int i = endY -  1; i >= start +  1; --i)
        {
             int number = numbers[i][start];
            PrintNumber(number);
        }
    }
}

void PrintNumber( int number)
{
    cout << number <<  "\t";
}

// ====================测试代码====================
void Test( int columns,  int rows)
{
    printf( "Test Begin: %d columns, %d rows.\n", columns, rows);

     if(columns <  1 || rows <  1)
         return;

     int **numbers =  new  int*[rows];
     for( int i =  0; i < rows; ++i)
    {
        numbers[i] =  new  int[columns];
         for( int j =  0; j < columns; ++j)
        {
            numbers[i][j] = i * columns + j +  1;
        }
    }

    PrintMatrixClockwisely(numbers, columns, rows);
    printf( "\n");

     for( int i =  0; i < rows; ++i)
         delete[] ( int *)numbers[i];

     delete[] numbers;
}

int main()
{
     /*
    1    2    3    4    5
    6    7    8    9    10
    11   12   13   14   15
    16   17   18   19   20
    */

    Test( 54);

     return  0;
}

 

Makefile:

1
2
3
4
5
6
7
8
9
10
11
12
 
.PHONY:clean  
CPP=g++  
CFLAGS=-Wall -g  
BIN=test  
OBJS=PrintMatrix.o  
LIBS=  
$(BIN):$(OBJS)  
    $(CPP) $(CFLAGS) $^ -o $@ $(LIBS)  
%.o:%.cpp  
    $(CPP) $(CFLAGS) -c $< -o $@  
clean:  
    rm -f *.o $(BIN)  

 

运行结果:

Test Begin: 5 columns, 4 rows.
1 2 3 4 5 10 15 20 19 18
17 16 11 6 7 8 9 14 13 12
 
 

转载于:https://www.cnblogs.com/codemylife/p/3718317.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值