zoukankan      html  css  js  c++  java
  • 【面试题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 << " ";
    }

    // ====================测试代码====================
    void Test(int columns, int rows)
    {
        printf("Test Begin: %d columns, %d rows. ", 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(" ");

        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
     
     
  • 相关阅读:
    倒计时2(小于0时的格式)
    日期 Date()
    倒计时5(超过时间为0:0:0)
    倒计时4
    倒计时3
    Lucene_solr
    Solr与tomcat搭建(搭建好)
    SSM(Spring-SpringMvc-Mybatis)练习
    SpringMvc
    Mybatis(使用)与Spring整合
  • 原文地址:https://www.cnblogs.com/codemylife/p/3718317.html
Copyright © 2011-2022 走看看