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

算法描述:

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

算法实现:

/*************************************************************************
	> File Name: main.c
	> Author: cyf
	> Mail: XXX@qq.com
	> Created Time: 2016年05月13日 星期五 11时03分59秒
 ************************************************************************/

#include "printMatrixClockwise.h"

void Test(int columns, int rows)
{
    printf("Test Begin: %d columns, %d rows.\n", columns, rows);

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

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

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

	if (numbers != NULL)
	{
		for (i = 0; i < rows; i++)
		{
			if (numbers[i] != NULL)
				free(numbers[i]);
		}

		free(numbers);
	}

}
int main()
{
	Test(4,4);
	return 0;
}
/*************************************************************************
	> File Name: printMatrixClockwise.h
	> Author: cyf
	> Mail: XXX@qq.com
	> Created Time: 2016年05月13日 星期五 10时10分57秒
 ************************************************************************/

#ifndef _PRINTMATRIXCLOCKWISE_H
#define _PRINTMATRIXCLOCKWISE_H
#include <stdio.h>
#include <stdlib.h>

void printMatrixClockwise(int **numbers, int columns, int rows);


#endif
/*************************************************************************
	> File Name: printMatrixClockwise.c
	> Author: cyf
	> Mail: XXX@qq.com
	> Created Time: 2016年05月13日 星期五 10时11分51秒
 ************************************************************************/
#include "printMatrixClockwise.h"
/*
 * 输入一个矩阵,按照从外到里以顺时针的顺序依次打印每一个数字
 * */
void printMatrixInCircle(int **numbers, int columns, int rows, int start)
{
	int columnsLen = columns - 1 - start;
	int rowsLen = rows - 1 - start;
	int i = 0;
	//打印第一行
	for (i = start; i <=columnsLen; i++)
	{
		int number = numbers[start][i];
	    printf("%d ", number);	
	}
	//打印最后一列
	if (start < rowsLen)
	{
		for ( i = start + 1; i <= rowsLen; i++ )
		{
			int number = numbers[i][rowsLen];
			printf("%d ", number);
		}
	}
	//打印最后一行
	if (start < rowsLen && start <columnsLen)
	{
		for (i = columnsLen - 1; i >= start; i--)
		{
			int number = numbers[rowsLen][i];
			printf("%d ", number);
		}
	}
	//打印第一列
	if (start < rowsLen - 1 && start < columnsLen)
	{
		for (i = rowsLen - 1; i > start; i--)
		{
			int number = numbers[i][start];
			printf("%d ", number);
		}
	}
}
void printMatrixClockwise(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;
	}
}
.SUFFIXES:.c.o

CC = gcc
CFLAGS = -g -O2 -Wall
SRCS = main.c\
	   printMatrixClockwise.c
OBJS = $(SRCS:.c=.o)
EXEC = main

main:$(OBJS)
	$(CC) $(OBJS) -o $(EXEC) $(CFLAGS)

.c.o:
	$(CC) -o $@ -c $(CFLAGS) $<

clean:
	rm -rf $(OBJS) $(EXEC)





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yiluohan0307

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值