趋势科技之旋转矩阵

打印旋转矩阵

输入n,在一个n*n的矩阵中,从左上角顺时针输入1,2,3…;按正常输出;
输入:
3
输出:
1 2 3
8 9 4
7 6 5

#include <iostream>
#include <vector>
#include<iomanip>
using namespace std;

vector<vector<int>> printMatrix(int n)
{
	vector<vector<int>> res(n,vector<int>(n,0));
	int cols = n;
	int rows =n;
	//if (cols <= 0 || rows <= 0) return res;

	int start = 0;
	int k=0;
	while (cols > start * 2 && rows > start * 2)
	{
		int endX = cols - 1 - start;
		int endY = rows - 1 - start;

		//从左到右
		for (int i = start; i <= endX; i++)
			res[start][i] = ++k;
			//res.push_back(++k);

		//从上到下
		if (endY > start)
		{
			for (int i = start + 1; i <= endY; i++)
				//res.push_back(matrix[i][endX]);
			res[i][endX] = ++k;
		}


		//从右到左
		if (endY > start&&endX > start)
		{
			for (int i = endX - 1; i >= start; i--)
				//res.push_back(matrix[endY][i]);
			res[endY][i] = ++k;
		}

		//从下到上
		if (endY > start + 1 && endX > start)
		{
			for (int i = endY - 1; i >= start + 1; i--)
				//res.push_back(matrix[i][start]);
			res[i][start] = ++k;
		}

		start++;
	}
	return res;
}

int main()
{
	int n;
	while (cin >> n)
	{
		vector<vector<int>> res = printMatrix(n);
		for (auto num : res)
		{
			for (auto n : num)
				cout <<setw(3)<<setiosflags(ios::left)<<n ;
			cout << endl;
		}
		cout << endl;
	}
	system("pause");
	return 0;
}

打印旋转矩阵2
输入:
1 2 3
4 5 6
7 8 9
输出:
1 2 3 6 9 8 7 4 5

#include <iostream>
#include <vector>
 
using namespace std;
 
vector<int> printMatrix(vector<vector<int> > matrix) 
{
	vector<int> res;
	if (matrix.size() == 0) return res;
	int cols = matrix[0].size();
	int rows = matrix.size();
	if (cols <= 0 || rows <= 0) return res;
 
	int start = 0;
	
	while (cols > start * 2 && rows > start * 2)
	{
		int endX = cols - 1 - start;
		int endY = rows - 1 - start;
 
		//从左到右
		for (int i = start; i <= endX; i++)
			res.push_back(matrix[start][i]);
 
		//从上到下
		if (endY > start)
		{
			for (int i = start + 1; i <= endY; i++)
				res.push_back(matrix[i][endX]);
		}
			
 
		//从右到左
		if (endY > start&&endX > start)
		{
			for (int i = endX - 1; i >= start; i--)
				res.push_back(matrix[endY][i]);
		}
 
		//从下到上
		if (endY > start + 1 && endX > start)
		{
			for (int i = endY - 1; i >= start + 1; i--)
				res.push_back(matrix[i][start]);
		}
 
		start++;
	}
	return res;
}
 
int main()
{
	vector<vector<int> > matrix = { { 1, 2, 3 }, {4, 5, 6 }, { 7, 8, 9 }};
	vector<int> res = printMatrix(matrix);
	for (auto num : res)
		cout << num << " ";
	cout << endl;
 
	system("pause");
	return 0;
}

华为笔试之旋转矩阵
从左上角开始旋转读数,遇到个位为7且十位为奇数则读出坐标;

#include<iostream>
#include<vector>
#include<map>
#include<utility>
#include<string>
#include<sstream>
using namespace std;

bool bingo(int n)
{
	
	if (n%10==7&&((n%100)/10)%2!=0)
		return true;
	return false;
}

int main()
{
	int N, M;
	while (cin >> N >> M)
	{
		vector<vector<int>> vec(N, vector<int>(M, 0));
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < M; j++) {
				cin >> vec[i][j];
			}
		}
		vector<pair<int, int>> res;
		int start = 0;
		while (start * 2 < N&&start * 2 < M) {
			int endX = N - start - 1;
			int endY = M - start - 1;
			for (int i = start; i <= endX; i++) {
				if (bingo(vec[start][i])) {
					res.push_back({start,i});
				}
			}
			if (start < endY) {
				for (int i = start + 1; i <= endY; i++) {
					if (bingo(vec[i][endX])) {
						res.push_back({ i,endX });
					}
				}
			}
			if (start < endY&&start < endX) {
				for (int i = endX - 1; i >= start; i--) {
					if (bingo(vec[endY][i])) {
						res.push_back({ endY,i });
					}
				}
			}
			if (start+1 < endY&&start < endX) {
				for (int i = endY - 1; i >= start+1; i--) {
					if (bingo(vec[i][start])) {
						res.push_back({ i,start });
					}
				}
			}
			start++;
		}
		cout << "[";
		for (int i=0;i<res.size();i++)
		{
			if(i!=res.size()-1)
				cout << "[" << res[i].first << "," << res[i].second << "],";
			else
				cout << "[" << res[i].first << "," << res[i].second << "]";
		}
		cout << "]" << endl;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值