C++经典编程题目(六)倒填数 蛇形填数 回转填数

6. 矩阵中填数. 当给出 N*N 的矩阵,要求用程序填入下列形式的数:
   ① 倒填,例如N=5             ② 蛇形填数              ③ 回转填数
#include <iostream>
#include <stdio.h>

using namespace std;

/*
本质上是一个按照规律构建二维矩阵的问题
*/

void Inversion(int n)
{
	//只有一个数的时候输出1
	if (n == 1)
	{
		printf("1");
	}

	//动态生成一个二维数组
	int **shouList = new int*[n];//开辟行
	for (int i = 0; i < n; i++)
		shouList[i] = new int[n]; //开辟列

	int max = n*n;

	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			shouList[i][j] = max;
			max--;
		}
	}

	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			printf("%4d", shouList[i][j]);
		}
		printf("\n");
	}
	printf("\n");
}

void Snake(int n)
{
	//只有一个数的时候输出1
	if (n == 1)
	{
		printf("1");
	}

	//动态生成一个二维数组
	int **shouList = new int*[n];//开辟行
	for (int i = 0; i < n; i++)
		shouList[i] = new int[n]; //开辟列

	int row = 0, col = 0;
	int Filler = 1;
	int loop = 0;

	for (size_t loop = 0; loop < 2*n-1; loop++)
	{
		row = (loop < n) ? 0 : loop - (n - 1);
		col = (loop < n) ? loop : (n - 1);
		for (int j = row; j <= col; j++)
			(loop % 2) ? (shouList[j][loop - j] = Filler++) : (shouList[loop - j][j] = Filler++);
	}

	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			printf("%4d", shouList[i][j]);
		}
		printf("\n");
	}
	printf("\n");
}

void Rotation(int n)
{
	//只有一个数的时候输出1
	if (n == 1)
	{
		printf("1");
	}

	//动态生成一个二维数组
	int **shouList = new int*[n];//开辟行
	for (int i = 0; i < n; i++)
		shouList[i] = new int[n]; //开辟列

	int Filler = 1;
	int head = 0, tail = n-1;
	int row = 0, col = 0;

	for (int i = 0; i <= n / 2; i++)
	{

		if (head == tail)
		{
			shouList[head][tail] = Filler;
		}
		for (row = head; row < tail; row++)
		{
			shouList[row][col] = Filler++;
		}
		for (col = head; col < tail; col++)
		{
			shouList[row][col] = Filler++;
		}
		for (row = tail; row > head; row--)
		{
			shouList[row][col] = Filler++;
		}
		for (col = tail; col> head; col--)
		{
			shouList[row][col] = Filler++;
		}
		head++; 
		tail--;
		row = head;
		col = head;
	}

	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			printf("%4d", shouList[i][j]);
		}
		printf("\n");
	}
	printf("\n");
}

int main()
{
	int n = 0,choose = 0;

	cout << "Please input the dimension you want to Constructing the Matrix :" << endl;
	cin >> n;
	cout << "Please choose the Matrix:" << endl;
	cout << "1. Inversion ;" << endl;
	cout << "2. Snake ;" << endl;
	cout << "3. Rotation ;" << endl;
	cin >> choose;

	switch (choose)
	{
	case 1:
		Inversion(n);
		break;
	case 2:
		Snake(n);
		break;
	case 3:
		Rotation(n);
		break;
	default:
		cout << "Error!" << endl;
		break;
	}

	system("pause");
	return 0;
}

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值