输出一个顺时递增数据的N阶矩阵

这篇博客讨论了如何输出一个N阶顺时递增矩阵,并提供了代码实现。作者指出这个看似简单的任务其实很容易出错,特别是面试时要徒手写出无bug的代码。文章还提到了矩阵的旋转问题,并给出了旋转后的矩阵示例。最后,博主提到了在B站看到的该题目的扩展,表示会后续研究。
摘要由CSDN通过智能技术生成

注意:这题虽然实现原理简单,但是非常非常容易写错。本人也调试了好一阵子,太容易写错了。

如果要求面试中徒手写出bug free的代码,除非事先很熟悉不然真的很容易写错。


输出一个N阶矩阵

比如输入1

输出

1


输入2

输出

1 2

4 3


输入 3

输出

1 2 3

8 9 4

7 6 5


代码如下

#define  _CRT_SECURE_NO_WARNINGS  
#include <tchar.h>  
#include <windows.h>  
#include <stdio.h>  

#define MAX_ORDER  255    
int array[MAX_ORDER][MAX_ORDER] = { 0 };

int _tmain(int argc, TCHAR* argv[], TCHAR * env[])
{

	int num;
	printf("Please the N order matrix of N:\n");
	scanf("%d", &num);

	int i = 0;          // indicate x-coordinate    
	int j = 0;          // indicate y-coordinate    
	int s = 1;          // the value of the array element    
	int n = num;        // the high bound for loop    
	int m = -1;         // the low bound for loop  

	i = -1;             // init the start point  
	j = -1;             // init the start point  

	while (m < n) {
		for (++i, ++j; j < n && (array[i][j] == 0); j++) {
			array[i][j] = s++;
		}

		for (--j, ++i; i < n && (array[i][j] == 0); i++) {
			array[i][j] = s++;
		}

		for (--i, --j; j > m && (array[i][j] == 0); j--) {
			array[i][j] = s++;
		}

		for (++j, --i; i > m && (array[i][j] == 0); i--) {
			array[i][j] = s++;
		}
		n--;
		m++;
	}

	// print the structure of the N-order Matrix.  
	printf("The %d-order Matrix is:\n", num);
	for (i = 0; i < num; i++){
		for (j = 0; j < num; j++)
			printf("%4d", array[i][j]);
		printf("\n");
	}

	return 0;
}

输出结果。


2)以上题目进行变换,可以从中间旋转出来,例如

输入3

输出

9 8 7

2 1 6

3 4 5

修改后的代码。

#define  _CRT_SECURE_NO_WARNINGS  
#include <tchar.h>  
#include <windows.h>  
#include <stdio.h>  

#define MAX_ORDER  255    
int array[MAX_ORDER][MAX_ORDER] = { 0 };

int _tmain(int argc, TCHAR* argv[], TCHAR * env[])
{

	int num;
	printf("Please the N order matrix of N:\n");
	scanf("%d", &num);

	int i = 0;          // indicate x-coordinate    
	int j = 0;          // indicate y-coordinate    
	int s = num * num;          // the value of the array element    
	int n = num;        // the high bound for loop    
	int m = -1;         // the low bound for loop  

	i = -1;             // init the start point  
	j = -1;             // init the start point  

	while (m < n) {
		for (++i, ++j; j < n && (array[i][j] == 0); j++) {
			array[i][j] = s--;
		}

		for (--j, ++i; i < n && (array[i][j] == 0); i++) {
			array[i][j] = s--;
		}

		for (--i, --j; j > m && (array[i][j] == 0); j--) {
			array[i][j] = s--;
		}

		for (++j, --i; i > m && (array[i][j] == 0); i--) {
			array[i][j] = s--;
		}
		n--;
		m++;
	}

	// print the structure of the N-order Matrix.  
	printf("The %d-order Matrix is:\n", num);
	for (i = 0; i < num; i++){
		for (j = 0; j < num; j++)
			printf("%4d", array[i][j]);
		printf("\n");
	}

	return 0;
}


扩展,在B站上看到这题的一个扩展,先将题目记录下来,后续研究。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值