笔试代码题--米哈游--顺时针打印矩阵字母

笔试代码题--米哈游--顺时针打印矩阵字母


题目要求:输入矩阵的行和列,然后顺时针顺出打印字母A-Z。

输入 1:

    1  1

输出1:

A   A

输入2:

   5     3

输出2:

A   B  C  D  E

L   M  N O  F

K   J   I  H  G

输入3:

   5     3

输出3:

A   B  C  D  E

L   M  N O  F

K   J   I  H  G

输入4:

10  12

输出4:

代码如下:

#include <stdio.h>
#include <string>
#include <iostream>
#include <vector>
using namespace std;

int main()
{
	int m,n;
	cin >> m >> n;

	vector<vector<char>> vec1(m,vector<char>(n));

	string s = "ABCDEFGHIJKLIMNOPQRSTUVWXYZ";
	int index = 0;
	int left = 0,top = 0,right = n - 1 ,bottom = m-1;


	while(left <= right && top <= bottom)
	{
		for(int i = left;i <= right; i++)
		{
			vec1[top][i]= s[index++];
			index %= 26;
		}
		for(int i = top + 1;i <= bottom; i++)
		{
			vec1[i][right]= s[index++];
			index %= 26;
		}
		if(top != bottom)
		{
			for(int i = right-1;i >= left; --i)
			{
				vec1[bottom][i]= s[index++];
				index %= 26;
			}
		}
		if(left != right)//下到上
		{
			for(int i = bottom - 1;i >top; --i)
			{
				vec1[i][left]= s[index++];
				index %= 26;
			}
		}
		left++,top++,right--,bottom--;
	}

	for(int i = 0; i < m;i++)
	{
		for(int j = 0;j < n;j++)
		{
	       cout  << vec1[i][j] << " ";
	     }
		cout << endl;
	}
	return 0;
}

结果1:

结果2:

结果3:

猜你喜欢

转载自blog.csdn.net/qq_41103495/article/details/108687422