打印矩阵、旋转矩阵、矩阵查找(c++)


不能想如何进行坐标位置变换到下一个,很难。
应该考虑数据状况,从宏观上进行分解。

1 转圈打印矩阵

1.1 打印一空心圈

#include <iostream>
using namespace std;
void printH(int** a, int aR, int aC, int bR, int bC)
{
	int indexac = aC;
	int indexar = aR;
	int indexbc = bC;
	int indexbr = bR;
	while (aC<bC)
		cout << *((int*)a + (bC- indexac+1) * aR + aC++) << " ";	// 
	while (aR < bR)
		cout << *((int*)a + (bC - indexac + 1) * (aR++) + aC) << " ";	// 
	while (aC > indexac)
		cout << *((int*)a + (bC - indexac + 1) * aR + aC--) << " ";	// 
	while (aR > indexar)
		cout << *((int*)a + (bC - indexac + 1) * (aR--) + aC) << " ";	//
	cout << endl;
}

int main()
{
	int a[3][4] = { {1,2,3,4},{5,6,7,8},{9,10,11,12} };
	for (int i = 0; i < 3; i++)
		for (int j = 0; j < 3; j++)
			cout << a[i][j];
	cout << endl;
	printH((int**)a, 0, 0, 2, 3);
	system("pause");
	return 0;
}

在这里插入图片描述

1.2 打印函数

若传入数组为二维(int**)a,且不能确定长和宽。
way1:下面方法也能获取长度

1 int lines = sizeof(a) / sizeof(a[0][0]);
2 int row = sizeof(a) / sizeof(a[0]);
3 int column = lines / row;

way2:f为列数

int a[s][f];
 a[i][j] = *((int*)a + (f ) * i + j)

当二维数组不是在子函数中时,可以用1的方法求长度,在子函数中则不能求,可用vector方法。后续补上。

#include <iostream>
using namespace std;
void getlength(int** a)
{
	int lines = sizeof(a) / sizeof(*((int*)a));
	int row = sizeof(a) / sizeof((int*)a);
	int column = lines / row;
	cout << row << " " << column << endl;
}
int main()
{
	
	int a[3][3] = { {1,2,3},{3,4,5},{3,4,5} };

	//int lines = sizeof(a) / sizeof(a[0][0]);
	//int row = sizeof(a) / sizeof(a[0]);
	//int column = lines / row;
	//cout << row << " " << column << endl;

	getlength((int**)a);
	system("pause");
	return 0;
}
#include <iostream>
using namespace std;
//f为求二维动态数组的值
void printH(int** a, int aR, int aC, int bR, int bC,int f)
{
	int indexac = aC;
	int indexar = aR;
	int indexbc = bC;
	int indexbr = bR;

	if (indexar == indexbr)
		while (aC <= bC)
			cout << *((int*)a + (f + 1) * aR + aC++) << " ";	// 
	else if (indexac == indexbc)
		while (aR <= bR)
			cout << *((int*)a + (f + 1) * (aR++) + aC) << " ";
	else
	{
		while (aC < bC)
			cout << *((int*)a + (f + 1) * aR + aC++) << " ";	// 
		while (aR < bR)
			cout << *((int*)a + (f + 1) * (aR++) + aC) << " ";	// 
		while (aC > indexac)
			cout << *((int*)a + (f + 1) * aR + aC--) << " ";	// 
		while (aR > indexar)
			cout << *((int*)a + (f + 1) * (aR--) + aC) << " ";	//
		//cout << endl;
	}
}
//a[5][5],则输入i为4,j为4
void printAll(int** a, int i, int j)
{
	int ac = 0, ar = 0;
	int bc = j, br = i;
	while (ac <= bc && ar <= br)
		printH(a, ar++, ac++, br--, bc--,j);
}
int main()
{
	int a[5][5] = { {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} };
	for (int i = 0; i < 5; i++)
		for (int j = 0; j < 5; j++)
			cout << a[i][j];
	cout << endl;
	//printH((int**)a, 0, 0, 2, 3);
	printAll((int**)a, 4, 4);
	system("pause");
	return 0;
}

2 旋转正方形矩阵:vector方法(后续更新)

#include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> matrix;

void print(vector<vector<int>> &a, int bound_c, int bound_r, int cols, int rows)
{
	int time = cols - bound_c;
	int cur_c = bound_c;
	int cur_r = bound_r;
	for (int i = 0; i != time; i++)
	{
		int temp = a[cur_r][cur_c + i];
		a[cur_r][cur_c + i] = a[rows - i][cur_c];
		a[rows - i][cur_c] = a[rows][cols - i];
		a[rows][cols - i] = a[cur_r + i][cols];
		a[cur_r + i][cols] = temp;
	}
}
void rotation(vector<vector<int>> &a)
{
	int rows = a.size() - 1;
	int cols = a[0].size() - 1;
	int bound_r = 0;
	int bound_c = 0;

	while (bound_c < cols && bound_r < rows)
	{
		print(a, bound_c++, bound_r++, cols--, rows--);
	}


}


int main()
{
	
	int a[6][6] = { {2, 29, 20, 26, 16, 28}, {12, 27, 9, 25, 13, 21},{32, 33, 32, 2, 28, 14},{13, 14, 32, 27, 22, 26},{33, 1, 20, 7, 21, 7},{4, 24, 1, 6, 32, 34} };
	
	for (int i = 0; i < 6; i++)
	{
		vector<int> cur;
		for (int j = 0; j < 6; j++)
		{
			//vector<int> cur;
			cur.push_back(a[i][j]);
		}
		matrix.push_back(cur);
	}

	rotation(matrix);

	for (int i = 0; i < 6; i++)
	{
		for (int j = 0; j < 6; j++)
		{
			cout << matrix[i][j] << " ";
		}
	}
	cout << endl;
	system("pause");
	return 0;
}

3 之打印(后续更新)

4 矩阵查找(后续更新)

在这里插入图片描述

class Solution {
public:
    bool findNumberIn2DArray(vector<vector<int>>& matrix, int target) 
    {
        if (matrix.empty())
            return false;
        int row = matrix.size() - 1;
        int col = matrix[0].size() - 1;
        int cur = 0;
        while (cur<= row && col >= 0)
        {
            if (matrix[cur][col] > target)
            {
                col--;     
            }
            else if (matrix[cur][col] < target)
            {
                cur++;
            }
            else
            {
                return true;
            }
        }
        return false;
    }
};
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值