面试题29:顺时针打印矩阵

一、题目

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

二、关键

1.思考最后一圈的打印情况和打印的处理方式。

2.循环结束的条件。

三、解释

1.将矩阵想成若干个圈,使用一个循环来打印矩阵,每次打印矩阵中的一个圈。

2.循环结束的条件:假设矩阵的行数是rows,矩阵的列数是colums。每一圈的左上角为(startX,startY)(根据实际情况分析出startX=startY),那么循环继续的条件是coloums>2*startX并且rows>startY*2。

3.最后一圈可能退化成只有一行、只有一列、只有一个数字等情况。

需要一步:只有一行

需要两步:只有一列

需要三步:至少两行两列

需要四步:至少三行两列

规律:
  第一步总是需要的。

  如果只有一行,那就不用第二步了。所以需要进行第二步的前提条件是终止行号大于起始行号

  需要第三步的条件是圈内至少有两行两列。即:终止行号大于起始行号,并且终止列号大于起始列号

  需要进行第四步的条件是:至少有三行两列,因此要求终止行号比起始行号至少大于等于2,同时终止列号大于起始列号

四、代码

#include <cstdio>

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

void PrintMatrixClockwisely(int** numbers, int columns, int rows)
{
    if(numbers == nullptr || 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)
{
    printf("%d\t", number);
}

// ====================测试代码====================
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(int argc, char* argv[])
{
    /*
    1    
    */
    Test(1, 1);

    /*
    1    2
    3    4
    */
    Test(2, 2);

    /*
    1    2    3    4
    5    6    7    8
    9    10   11   12
    13   14   15   16
    */
    Test(4, 4);

    /*
    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
    */
    Test(5, 5);

    /*
    1
    2
    3
    4
    5
    */
    Test(1, 5);

    /*
    1    2
    3    4
    5    6
    7    8
    9    10
    */
    Test(2, 5);

    /*
    1    2    3
    4    5    6
    7    8    9
    10   11   12
    13   14   15
    */
    Test(3, 5);

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

    /*
    1    2    3    4    5
    */
    Test(5, 1);

    /*
    1    2    3    4    5
    6    7    8    9    10
    */
    Test(5, 2);

    /*
    1    2    3    4    5
    6    7    8    9    10
    11   12   13   14    15
    */
    Test(5, 3);

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

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
非常感谢您的提问,以下是我的回答: 1. Spring Boot的自动配置原理: Spring Boot的自动配置是基于条件注解实现的。Spring Boot会根据classpath中的jar包、类、属性等信息,来决定是否需要给容器中添加配置类。如果需要添加,则会根据条件自动配置底层框架,如Tomcat、Redis、MySQL等。如果不需要添加,则不会添加。 2. Spring Boot导入其他的配置文件: Spring Boot可以通过在application.properties或application.yml文件中添加spring.config.import属性来导入其他的配置文件。例如,可以添加以下内容来导入另一个配置文件: ``` spring.config.import=classpath:/another-config.yml ``` 3. Spring Boot的核心注解: Spring Boot的核心注解包括@SpringBootApplication、@Configuration、@EnableAutoConfiguration和@ComponentScan。其中@SpringBootApplication是一个复合注解,包含了@Configuration、@EnableAutoConfiguration和@ComponentScan注解的功能。 4. Spring Boot的实现: Spring Boot的实现基于Spring框架,它使用了大量的注解来简化配置。它还使用了条件注解来根据不同条件自动配置底层框架。另外,Spring Boot还提供了很多starter包,可以让开发人员更加方便快速地搭建应用程序。 5. Spring Boot和Spring MVC的区别: Spring Boot是一个快速构建基于Spring的应用程序的框架,它使用了很多注解来简化配置。而Spring MVC是一个基于MVC架构的Web应用程序框架,它是Spring框架的一部分。Spring Boot可以使用Spring MVC来构建Web应用程序,但是它还可以用于构建其他类型的应用程序。 6. Spring Boot如何跨域请求: Spring Boot可以通过添加一个跨域请求的过滤器来实现跨域请求。具体来说,可以创建一个类实现javax.servlet.Filter接口,然后在类上添加@WebFilter注解,并设置urlPatterns属性来指定需要跨域请求的URL。在过滤器实现的doFilter方法中,设置Access-Control-Allow-Origin、Access-Control-Allow-Methods、Access-Control-Allow-Headers和Access-Control-Max-Age等跨域请求头信息即可。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值